为什么我的IF语句不适用于python?

时间:2017-08-04 14:12:18

标签: python

import random
circlediameter = random.randint(1,99)
pi = 3.142
print("The circle diameter is",circlediameter,"   Find the area")

#find 4 possible answers
circleanswer1 = circlediameter/pi
circleanswer2 = circlediameter*pi
circleanswer3 = circlediameter+pi
circleanswer4 = circlediameter

#makes sure all numbers are integers
circleanswer1 = int(circleanswer1) 
circleanswer2 = int(circleanswer2)
circleanswer3 = int(circleanswer3)
circleanswer4 = int(circleanswer4)

answerlist = [circleanswer1 , circleanswer2 , circleanswer3 , circleanswer4]

#shuffles the list
random.shuffle(answerlist) 




#answer 2 is always the right answer
print("Four options are:    ", answerlist)


#user enters a number
useranswercircle = input("Please choose an answer:   ")
int(useranswercircle)


str(useranswercircle)
str(circleanswer2)

if useranswercircle == circleanswer2:
print("EEE")

我正在比较两个整数,为什么我的IF语句不起作用 比较两个数字。我已将两个变量转换为整数,但仍然没有成功比较

3 个答案:

答案 0 :(得分:0)

尝试使用此演员:

import random
circlediameter = random.randint(1,99)
pi = 3.142
print("The circle diameter is",circlediameter,"   Find the area")

#find 4 possible answers
circleanswer1 = circlediameter/pi
circleanswer2 = circlediameter*pi
circleanswer3 = circlediameter+pi
circleanswer4 = circlediameter

#makes sure all numbers are integers
circleanswer1 = int(circleanswer1) 
circleanswer2 = int(circleanswer2)
circleanswer3 = int(circleanswer3)
circleanswer4 = int(circleanswer4)

answerlist = [circleanswer1 , circleanswer2 , circleanswer3 , circleanswer4]

#shuffles the list
random.shuffle(answerlist) 


#answer 2 is always the right answer
print("Four options are:    ", answerlist)


#user enters a number
useranswercircle = input("Please choose an answer:   ")

# EDIT:
# HERE IS THE TRICK
useranswercircle = int(useranswercircle)

if useranswercircle == circleanswer2:
    print("EEE")

答案 1 :(得分:0)

你需要缩进印刷品:

if useranswercircle == circleanswer2:
    print("EEE")

答案 2 :(得分:0)

试试这个 -

if str(useranswercircle) == str(circleanswer2):
    print("EEE")

删除这两个陈述 -

str(useranswercircle)
str(circleanswer2)