我的代码没有运行过“while循环”

2024-06-16 03:01:29 发布

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

password = input('Do you wish to add a camper, answer with yes or no:')
while password == 'no':
    paid = int(input('How much money have you paid:'))
    name = input('What is the camper\'s name:')
    
    if paid == '120':
        print(name, 'Your sport is football')
    
    if paid == '200':
        print(name, 'Your sport is Basketball')
        password = str(input('Do you wish to add a camper, answer with yes or no:'))
    
    if paid == '225':
        print(name, 'Your sport is Netball')
        password = str(input('Do you wish to add a camper, answer with yes or no:'))
    
    if paid == '275':
        print(name, 'Your sport is Hockey')
        password = str(input('Do you wish to add a camper, answer with yes or no:'))
    
    if paid == '300':
        print(name, 'Your sport is swimming')
        password = str(input('Do you wish to add a camper, answer with yes or no:'))

输出

 runfile('C:/Users/camro_803vhwj/Desktop/ITSBA inpython.py', wdir='C:/Users/camro_803vhwj/Desktop')

Do you wish to add a camper, answer with yes or no:yes

当我需要所有3个提示时,上面的输出是唯一的输出。 如何获得获取的提示;姓名、付款和密码是否会一个接一个发生


Tags: ortonoanswernameyouaddinput
3条回答

不确定你想让程序做什么,但可能是这个:

pw=input('Do you wish to add a camper, answer with yes or no:')
while pw=='no':
    paid=int(input('How much money have you paid:'))
    name=input('What is the camper\'s name:')
    if paid==120:
        print(name, 'Your sport is football')
    else:
        if paid==200:
            print(name, 'Your sport is Basketball')
        elif paid==225:
            print(name, 'Your sport is Netball')
        elif paid==275:
            print(name, 'Your sport is Hockey')
        elif paid==300:
            print(name, 'Your sport is swimming')
        pw=input('Do you wish to add a camper, answer with yes or no:')

我相信下面的代码可以完成这项工作

password = input('Do you wish to add a camper, answer with yes or no:')
while password == 'yes':
    paid = int(input('How much money have you paid:'))
    name = input('What is the camper\'s name:')

    if paid == 120:
        print(name, 'Your sport is football')
    elif paid == 200:
        print(name, 'Your sport is Basketball')
    elif paid == 225:
        print(name, 'Your sport is Netball')
    elif paid == 275:
        print(name, 'Your sport is Hockey')
    elif paid == 300:
        print(name, 'Your sport is swimming')
    else: 
        print (name, "Unknown")        
    password = str(input('Do you wish to add a camper, answer with yes or no:'))

print ('End of Program.')

我认为“else”是必要的,但如果您愿意,请删除它

当密码==“否”时更改此行:将密码更改为while password!='否: 如果用户输入字符串“no”,则退出循环

相关问题 更多 >