5 Replies - 716 Views - Last Post: 03 February 2012 - 10:03 AM Rate Topic: -----

#1 ATC3  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 04-October 11

What is wrong with this code?

Posted 03 February 2012 - 09:00 AM

As far as I can tell I cannot find a way to fix this:

import getpass

def pass_entry:
	p = getpass.getpass(prompt='What is your favorite color? ')
	if p.lower() == 'blue':
    	print 'Right.  Off you go.'
		check = 1
	else:
    	print 'Auuuuugh!'

def check_valid:
	if check = 1:
	print "Hello, Alex"

pass_entry()
check_valid()


I have been having trouble with python lately, mainly due to the fact that I am out of practice. I started in android development so that is why I have been away from python. Any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: What is wrong with this code?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: What is wrong with this code?

Posted 03 February 2012 - 09:10 AM

In the future it would be helpful if you included the error message(s), you received. But here's what I can see from a glance:

You can't leave out the parameter list when defining a function (or when calling it for that matter) - even if it's empty (lines 3 and 11).

Use == for comparisons, not = (line 12).

The variable check is local to pass_entry, so you can't use it inside check_valid (line 12). One fix would be to make it global, but that's a bad idea. Rather you should return it from pass_entry and pass it as a parameter to check_valid.

You did not indent the body of the if statement (line 13).
Was This Post Helpful? 1
  • +
  • -

#3 ATC3  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 04-October 11

Re: What is wrong with this code?

Posted 03 February 2012 - 09:23 AM

Like this:

import getpass
def pass_entry():
	p = getpass.getpass(prompt='What is your favorite color? ')
	if p.lower() == 'blue':
    	print 'Right.  Off you go.'
		return check = 1
	else:
    	print 'Auuuuugh!'

def check_valid(check):
	if check == 1:
		print "Hello, Alex"

pass_entry()
check_valid()

Because now I get this error message:


File "test.py", line 6
print 'Right. Off you go.'
^
Indentationerror: expected an indented block

I have really forgotten python for some reason.

This post has been edited by ATC3: 03 February 2012 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,677
  • Joined: 25-December 09

Re: What is wrong with this code?

Posted 03 February 2012 - 09:51 AM

You seem to be mixing spaces with your tabs. You should either use all spaces or all tabs.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 ATC3  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 04-October 11

Re: What is wrong with this code?

Posted 03 February 2012 - 10:00 AM

Well after I fix that I still can't figure out the check part.
Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: What is wrong with this code?

Posted 03 February 2012 - 10:03 AM

Your return statement is invalid syntax, you can't return an assignment:

if p.lower() == 'blue':
    print 'Right.  Off you go.'
    return check = 1



I imagine you want:

if p.lower() == 'blue':
    print 'Right.  Off you go.'
    check = 1
    return check


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1