Input()无法正常工作

时间:2018-07-18 23:03:30

标签: python python-3.x

我试图创建这款剪刀石头布游戏。但是输入无效。有人知道我在做什么错吗?每次我尝试输入时,都会显示以为我没有输入任何内容或没有输入数字1-3,然后直接进入其他位置。 随机导入

import time

y=3

while y>0:

    print("Lets play Rock Paper Scissors")
    print("1 for Rock")
    print("2 for Paper")
    print("3 for Scissors")
    time.sleep(1)
    print("Rock, Paper, Scissors...")
    var=input()
    z=random.randint(1,3)
    if z==1:
        print("I play Rock")
        time.sleep(1)
        if str(var)==1:
            print("You played Rock too! TIE")
        if str(var)==2:
            print("You played Paper! YOU WIN!!!")
        if str(var)==3:
            print("You played Scissors! I WIN!")
            y=y-1
        else:
            print("ERROR! Pick a number 1-3.")
    if z==2:
        print("I play Paper")
        time.sleep(1)
        if str(var)==1:
            print("You played Rock! I WIN!")
            y=y-1
        if str(var)==2:
            print("You played Paper too! TIE")
        if str(var)==3:
            print("You played Scissors! YOU WIN!!!")
        else:
            print("ERROR! Pick a number 1-3.")
    if z==3:
        print("I play Scissors")
        time.sleep(1)
        if str(var)==1:
            print("You played Rock! YOU WIN!!!")
        if str(var)==2:
            print("You played Paper too! I WIN!")
            y=y-1
        if str(var)==3:
            print("You played Scissors too! TIE")
        else:
            print("ERROR! Pick a number 1-3.")
    else:
        print("THIS SHOULDN'T BE POSSIBLE BUT OH WELL.... EASTER EGG")

1 个答案:

答案 0 :(得分:0)

您的input()工作正常。但是,您正在将字符串与整数进行比较。 在每个if中,您都在比较两种不同的数据类型。因为"1"1不同,所以您编写的程序无法正常工作。

您将必须将从输入中获取的字符串转换为数字(整数)。 您的ifs必须看起来像:

var = input()
if int(var) == 1:
    print("You type in 1")

如果不想为每个变量转换变量var,甚至更好:

var = int(input())
if var == 1:
    print("You typed 1")

有关更多信息,请参见:https://www.programiz.com/python-programming/variables-datatypes