【问题标题】:if input type by user is not defined how to add如果未定义用户的输入类型如何添加
【发布时间】:2022-11-23 12:51:02
【问题描述】:
import random

def roll_dice():
    dice_drawing = {
        1:(
            "_________",
            "|   1    |",
            "|   *    |",
            "----------"  
        ),
        2:(
            "__________",
            "|    2    |",
            "|   * *   |",
            "-----------"
        ),
        3:(
            
            "__________",
            "|    3    |",
            "|  * * *  |",
            "-----------"
        ),

        4:(
            "__________",
            "|    4    |",
            "| * * * * |",
            "-----------"
        ),

        5:(
            "__________",
            "|    5  * |",
            "| * * * * |",
            "-----------" 
        ),
        6:(

            "__________",
            "| *  6  * |",
            "| * * * * |",
            "-----------" 
         )

    }

    roll = input('Roll the dice Yes/No: ')
    while roll.lower() == 'yes'.lower():
        dice1 = random.randint(1,6)
        dice2 = random.randint(1,6)

        print('dice rolled: {} and {}'.format(dice1,dice2))
        print("\n".join(dice_drawing[dice1]))
        print("\n".join(dice_drawing[dice2]))
        roll = input('Roll the dice Yes/No: ')

        if roll not in roll:
            roll = input('Roll the dice Yes/No: ')
        
roll_dice()

我无法理解如果用户键入其他内容而不是 yesno,那么我希望迭代再次发生,说无效选项请键入是或否

这段代码工作正常,但是如果用户没有输入 yes 或 no 输入与我希望迭代再次运行不同的关键字,说它是一个无效选项,请输入 yes 或 no,当用户输入错误输入时如何添加它是由是或否定义

【问题讨论】:

标签: python


【解决方案1】:

这是你要找的吗?

while True:
    roll = input('Roll the dice Yes/No: ')
    if roll.lower() == 'yes':
        
        ##
        ## do your stuff here 
        ##

    elif roll.lower() =='no':
        break
    else :
        print('enter yes or no') 

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 2016-07-24
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2012-05-22
    • 2018-02-05
    相关资源
    最近更新 更多