[Tutor] creating a dictionary for capital quiz program
Peter Otten
__peter__ at web.de
Tue Jun 2 18:17:09 CEST 2015
Alan Gauld wrote:
> On 02/06/15 15:15, Peter Otten wrote:
>
>> Not an optimization, but if the user enters neither Y nor N you might ask
>> again instead of assuming Y.
>
> He does. He only breaks if the user enters N
>
>>> choice = input('Do you want to play again y/n: ')
>>> if choice.upper() == 'N':
>>> print('end of game')
>>> break
>>> elif choice.upper() != 'Y':
>>> print("invalid choice")
>
> Y goes round again silently.
> Anything other than Y or N prints the error then tries again.
... with the next state. I meant that instead the question "Do you want to
play again y/n:" should be repeated until there is a valid answer, either y
or n.
Current behaviour:
$ python capitals.py
Enter the capital of Mississippi :Jackson
Correct
Do you want to play again y/n: x
invalid choice
Enter the capital of Oklahoma :
...
So "x" is a synonum for "n".
Suggested behaviour:
$ python capitals.py
Enter the capital of Mississippi :Jackson
Correct
Do you want to play again y/n: x
invalid choice
Do you want to play again y/n: z
invalid choice
Do you want to play again y/n: n
end of game
...
More information about the Tutor
mailing list