[Tutor] if statement

Francesco Loffredo fal at libero.it
Thu Nov 4 20:02:41 CET 2010


On 02/11/2010 20.07, Glen Clark wrote:
> sorry:
>
> NumItems = int(input("How many Items: "))
>
>
> Entries = []
> for In in range(0,NumItems):
>     Entries.append("")
>     	
>
>
> for In in range(0,NumItems):
>     Entries[In]=str(input("Enter name " + str(In+1) + ": "))
>
>
> for In in range(0,NumItems):
>     print(Entries[In])
>
> confirmed = int(input("Are you happy with this? (y/n): ")
>
> if confirmed == "y":
>     for In in range(0,NumItems):
>        print(Entries[In] + ": " + str(In))
>     change = int(input("Which item would you like to change: ")
>     Entries[change]=str(input("Please enter a nem name: ")
> else:
>      #do nothing
>
> print(Entries)
>
>> On 11/2/10, Glen Clark<glenuk at gmail.com>  wrote:
>>>   File "/home/glen/workspace/test.py", line 19
>>>      if confirmed == "y":
>>>                         ^
>>> SyntaxError: invalid syntax

It seems to me that when you wrote the request for confirmation you just 
copied and pasted part of the first line, changing the variable name to 
'confirmed'. But you were expecting a string, so you should delete the 
leading "int(" part of that statement, what you forgot to do:

confirmed = input("Are you happy with this? (y/n): ")

So the problem was not in the missing ending parenthesis, but in those 
four characters in excess. A very similar mistake is in the request for 
a new name for Entries[change]: remember that input() ALWAYS returns a 
string.

Entries[change]=input("Please enter a new name: ")

Finally, as others have pointed out, you should put a pass statement 
after the else clause, or delete it altogether:
 > if confirmed == "y":
 >     for In in range(0,NumItems):
 >        print(Entries[In] + ": " + str(In))
 >     change = input("Which item would you like to change: ")
 >     Entries[change]=input("Please enter a nem name: ")
#the following two lines may be safely deleted, or must be BOTH there
 > else:
 >      pass

Hope that helps,
Francesco
-------------- next part --------------

Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com 
Versione: 9.0.864 / Database dei virus: 271.1.1/3235 -  Data di rilascio: 11/03/10 09:36:00


More information about the Tutor mailing list