选择问题&随机。蟒蛇

时间:2013-06-06 05:12:20

标签: python

所以我遇到了这个游戏的问题,当我运行程序时,无论哪个 用户输入的号码,他们将永远丢失。有人看到问题吗?

#python project, rock paper scissors game that responds to the user
 #must use if, while, for, list, be 50 lines long

import random
 from random import choice
 winList = []
 rock, paper, scissors = 1, 2, 3
 choiceOptions = [1, 2, 3]
 startOver = input("Do you want to play rock paper scissors?")
 startOver = startOver.lower()
 while startOver == "yes":
     name = input("What is your name?")
     print("What's good, ", name, ", welcome to the rock paper scissors game!")
     userChoice = input("It's your go! Rock(1), paper(2), or scissors(3)?")
     compChoice = choice(choiceOptions)
#if userChoice != "1" or userChoice != "2" or userChoice != "3":
    #print("That's not a valid choice, please choose a number from 1-3")
if compChoice == userChoice:
    print("You tied! No-one is the wiser!")
elif userChoice == 1 and compChoice == 3:
    print ("You win! The computer chose ", compChoice)
    winList.append(str(1))
    if userChoice == 2 and compChoice == 3:
        print ("You win! The computer chose ", compChoice)
        winList.append(str(1))
    if userChoice == 2 and compChoice == 1:
        print ("You win! The computer choice ", compChoice)
    if userChoice == 3 and compChoice == 2:
        print ("You win! The computer chose ", compChoice)
        winList.append(str(1))
        print ("You've won ", winList, "times!")
else:
    print ("You lose, try again! The computer chose ", compChoice)

2 个答案:

答案 0 :(得分:1)

您的一个问题是数据类型。在Python中,整数2不等同于字符串'2'。这一行就在这里:

userChoice = input("It's your go! Rock(1), paper(2), or scissors(3)?")

userChoice设置为字符串。您想将用户输入转换为整数:

userChoice = int(input("It's your go! Rock(1), paper(2), or scissors(3)?"))

现在,您的比较将像您最初的预期一样。

答案 1 :(得分:0)

这里的缩进是错误的:

elif userChoice == 1 and compChoice == 3:
    print ("You win! The computer chose ", compChoice)
    winList.append(str(1))
    if userChoice == 2 and compChoice == 3:
        print ("You win! The computer chose ", compChoice)
        winList.append(str(1))
    if userChoice == 2 and compChoice == 1:
        print ("You win! The computer choice ", compChoice)
    if userChoice == 3 and compChoice == 2:
        print ("You win! The computer chose ", compChoice)
        winList.append(str(1))
        print ("You've won ", winList, "times!")
else:
    print ("You lose, try again! The computer chose ", compChoice)

应该是

elif userChoice == 1 and compChoice == 3:
    print ("You win! The computer chose ", compChoice)
    winList.append(str(1))
elif userChoice == 2 and compChoice == 3:
    print ("You win! The computer chose ", compChoice)
    winList.append(str(1))
elif userChoice == 2 and compChoice == 1:
    print ("You win! The computer choice ", compChoice)
elif userChoice == 3 and compChoice == 2:
    print ("You win! The computer chose ", compChoice)
    winList.append(str(1))
    print ("You've won ", winList, "times!")
else:
    print ("You lose, try again! The computer chose ", compChoice)

(但是,你应该非常认真地阅读你的逻辑。还有一些错误,不一致和错别字。)

正如Blender所说,你忘了强制输入int:

userChoice = int(input("It's your go! Rock(1), paper(2), or scissors(3)?"))