【问题标题】:Quiz making using id/else statements and random variables/dictionaries/lists使用 id/else 语句和随机变量/字典/列表进行测验
【发布时间】:2016-02-22 20:12:27
【问题描述】:

Quiz making instructions

我开始了我的代码

from random import randint, shuffle

numbers = {1:'\u4e00', 2:'\u4e8c', 3:'\u4e09', 4:'\u56db', 5:'\u4e94', \
    6:'\u516d', 7:'\u4e03', 8:'\u516b', 9:'\u4e5d', 10:'\u5341'}

x=randint(1,10)



print('Welcome to our quiz on Chinese/Japanese numerals.')

input('What is the Value of ' +numbers[x]  +'?' )


if 1 == '\u4e00' : 
    print('Congratulations! Let us continue then, consider:')
else:
    print( 'Congratulations! Let us continue then, consider:')
    input (['\u4e00', '\u4e8c', '\u4e09', '\u56db','\u4e94', \
    '\u516d', '\u4e03', '\u516b', '\u4e5d', '\u5341'])
    input ('At which position is the value of ' +numbers[x] +'?' )

我对代码的 if 和 else 部分有疑问。我已经能够通过询问初始问题并插入随机汉字来设置测验的开头,但是我遇到了一个问题,即“错误答案,____等于_____”。声明的一部分。我如何确定用户在提示中插入的内容是正确的还是不正确的?

【问题讨论】:

  • if user_input == right_answer?除非您实际分配用户的输入,否则这会有点棘手......

标签: python if-statement random input


【解决方案1】:

我如何确定用户在提示中插入的内容是正确的还是不正确的

捕获变量中的输入值,并进行等价检验:

val = input('What is the Value of ' +numbers[x]  +'?' )

if val == x:
    # input is correct

else:
    # input is not correct

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 2014-03-05
    相关资源
    最近更新 更多