if / elif / else-statement不能与random.choice一起使用

时间:2016-02-07 18:18:30

标签: python python-3.x if-statement

import random
print('hello')
question1 = [' cbca', ' cbbca']



print("What's equal to" + random.choice(question1) + "?")
r = input()
if r in ('7,8', '7.8', '78') and question1 == ' cbca' : 

    print("That's correct!")

elif r in ('1', '1,0') and str(question1) == ' cbbca' :
    print("That's correct!")

else :
    print('r')

无论输入是什么,都始终使用else语句。为什么?

我知道这不会那么难,但我刚刚开始这个:)

2 个答案:

答案 0 :(得分:0)

你应该这样做:

if(r in('7,8','7.8','78'))和(question1 =='cbca'):

答案 1 :(得分:0)

您将question1声明为列表。在其上应用random.choice不会更改其值,因此以后它仍然是一个列表。将random.choice的结果放在变量中并比较此变量。

import random
print('hello')
question1 = [' cbca', ' cbbca']

question = random.choice(question1) # Remember the choice of random

print("What's equal to" + question + "?")
r = input()
if r in ('7,8', '7.8', '78') and question == ' cbca' :

    print("That's correct!")

elif r in ('1', '1,0') and question == ' cbbca' :
    print("That's correct!")

else :
    print('r')