python同时运行两个while循环

2024-04-27 18:01:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个命运之轮游戏,并试图让这两个代码同时运行 第一个while循环是检查它是元音还是辅音,第二个while循环是检查之前是否已经拾取了辅音

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 

Tags: infalsetrueinputifnotwhile辅音
2条回答

与两个循环都依赖于各自作用域内确定的条件不同,您可以有一个循环在满足条件时中断

while True:
   my_var = do_something()
   if my_var == 1 or my_var == 2:
       do_something_else()
       break

为了对同一输入进行两次验证检查,仅当输入通过所有验证时,才可以在不确定的while循环和break中使用ifelif语句:

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)))

演示:https://repl.it/@blhsing/UnwrittenSpanishInteger

相关问题 更多 >