2013-06-06 54 views
-3

所以我遇到了这个游戏的问题,当我运行这个程序时,不管用户输入哪个号码,他们总是会输。任何人都看到问题?选择与随机存在问题。 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) 

回答

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)?")) 

现在,你比较会像你原本打算。

+0

谢谢!男人,调试可以让人难以置信。 :) –

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)?")) 
+0

得到了那些下来,现在我只是有添加号码附加到列表的问题。我拿走了winList.append(str(1))并将其更改为winList.append(1)。如果我的winList中有[1,1,1,1,1],我怎么让他们加在一起说“你有5个胜利”? –

+0

@welcomeapollo为什么不只是有winCounter = 0,做winCounter + = 1? – Patashu

+0

是的,我想特别从列表中添加它们。 –