2013-06-06 4 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) 

(Тем не менее, вы должны доказательство очень внимательно прочитать логику Есть еще некоторые ошибки, несоответствия и опечатки.).

И как говорит Блендер, вы забыли принудить вход к int:

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

получил это, теперь у меня просто возникают проблемы с добавлением номеров, которые добавляются в список. Я забрал winList.append (str (1)) и изменил его на winList.append (1). Если у меня есть [1, 1, 1, 1, 1] в моем winList, как мне их добавить вместе, чтобы сказать «у вас 5 побед»? –

+0

@welcomeapollo Почему бы не просто winCounter = 0 и сделать winCounter + = 1? – Patashu

+0

Да, я хотел бы специально добавить их из списка. –

Смежные вопросы