JD2775
JD2775

Reputation: 3801

Dice simulator input - Python

I am trying to figure out why any input starting with 'y' keeps this game going....looking at the code it appears only 'y' or 'yes' should keep it going but I could enter 'yensdg', 'yyyy' etc and it still loops.

Any ideas? Thanks

from random import randint
repeat = True
while repeat:
    print('You rolled', randint(1,6))
    print('Do you want to roll again?')
    repeat = ('y' or 'yes') in input().lower()

Upvotes: 0

Views: 65

Answers (1)

Kshitij Mittal
Kshitij Mittal

Reputation: 2776

'y' in input().lower(), this statement will return true if 'y' is present anywhere in the input string. Like if the input is 'thisisarandominputwithy' it will return true, because it has 'y' in the end

Change the last line to:

repeat = input().lower() in ['y', 'yes']

Upvotes: 1

Related Questions