I am trying to make a wheel of fortune game, and trying to get both codes to run simultaneously the first while loop is to check whether it is a vowel or consonant, and the second while loop is to check whether the consonant is already picked previously
if currentguess in vowels:
consonantcheck = False
while consonantcheck == False:
currentguess = input('Not a consonant, please try again with a consonant: ')
if currentguess not in vowels:
consonantcheck = True
if currentguess in addedcons:
consalreadyadded = False
while consalreadyadded == False:
currentguess = input('The consonant is already guessed, pick another consonant: ')
if currentguess not in addedcons:
consalreadyadded = True
Instead of having two loops that both are dependent on conditions determined within their individual scopes, you can have one loop that you break out of when a condition is met.
while True:
my_var = do_something()
if my_var == 1 or my_var == 2:
do_something_else()
break
For the purpose of making two validation checks against the same input you can use if
and elif
statements in an indefinite while
loop and break
only when the input passes all validations:
addedcons = set()
while True:
while True:
currentguess = input('Enter a consonant: ')
if currentguess in 'aeiou':
print('Not a consonant, please try again with a consonant.')
elif currentguess in addedcons:
print('The consonant is already guessed, pick another consonant.')
else:
addedcons.add(currentguess)
break
print('Consonants guessed so far:', ', '.join(sorted(addedcons)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.