2013-06-06 57 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

是的,我想特別從列表中添加它們。 –