if语句没有执行

时间:2016-09-03 23:26:02

标签: python python-3.x

语言的新手和这个摇滚剪刀的东西是我在python中做过的第一件事,所以我意识到代码是低效的和nooby但任何指针都将受到赞赏!基本上我得到一个响应,好像没有if语句被执行并且发现= False在整个程序中保持这样!所以输出是"你画了对手"即使我从调试中知道MyChoice和aiChoice是有效的而不是绘图。

import time as t
import random as r
import os

os.system('@Color 0a')

aiWins = 0
MyWins = 0

Rock = 1
Paper = 2
Scissors = 3
found = False

#welcome text
print("\nWelcome to rock paper scissors")
t.sleep(1)
print("\nPlease enter a username")
user = input("> ")

def aiCheck(aiWins):
    if aiWins > 5:
        print("Unfortunately the computer has bested you this time! Try again.")

def myCheck(MyWins):
    if MyWins > 5:
        print("Congratulations you have won the game!")

def whowon(found, MyChoice, aiChoice, myWins, aiWins):
    print (MyChoice)
    print (aiChoice)    
    if MyChoice == 1 and aiChoice == 3:
        found = True
        t.sleep(2)
        print('You chose rock and your opponent chose scissors! You win!')
        MyWins = MyWins + 1
    elif MyChoice == 2 and aiChoice == 1:
        found = True
        t.sleep(2)
        print('You chose paper and your opponent chose rock! You win!')
        MyWins = MyWins + 1
    elif MyChoice == 3 and aiChoice == 2:
        found = True
        t.sleep(2)
        print ('You chose scissors and your opponent chose paper! You win!')
        MyWins = MyWins + 1
    elif MyChoice == 3 and aiChoice == 1:
        found = True
        t.sleep(2)
        print('You chose scissors and your opponent chose rock! You lose!')
        aiWins = aiWins + 1
    elif MyChoice == 1 and aiChoice == 2:
        found = True
        t.sleep(2)
        print('You chose rock and your opponent chose paper! You lose!')
        aiWins = aiWins + 1
    elif MyChoice == 2 and aiChoice == 3:
        found = True
        t.sleep(2)
        print ('You chose paper and your opponent chose scissors! You lose!')
        aiWins = aiWins + 1
    if found == False:
        print("You drew with your opponent")
    return found
    return MyWins
    return aiWins

print("\nOptions!")
t.sleep(1)
print('\n1. Rock')
print('2. Paper')
print('3. Scissors')
print('\nEnter the number that correlates with your choice')
MyChoice = input('> ')
aiChoice = r.randint(1,3)

whowon(found, MyChoice, aiChoice, MyWins, aiWins)

3 个答案:

答案 0 :(得分:1)

这可以解决您的问题:

t

您正在比较字符串(MyChoice)和整数(aiChoice)。

答案 1 :(得分:1)

input返回一个字符串,因此您必须使用整数转换器包装MyString,如下所示:

MyChoice = int(input("> "))

由于字符串无法与整数进行准确比较,found未设置为True,因此found为False,导致其报告平局。

接下来,您无法使用单独的返回语句返回多个内容,在这种情况下,由于您不对返回值执行任何操作,因此无需执行任何操作。如果您确实想要返回值,可以使用元组返回:

return (found, MyWins, aiWins)

注意:参数名称不必与全局变量相同。参数变量是局部变量,用作实际传入的占位符。还有冗余参数。发现,MyChoice和aiChoice不需要通过。

答案 2 :(得分:-1)

首先,这些已经是全局变量。无需将它们用作参数。

aiWins = 0
MyWins = 0
found = False

现在,您可以像这样定义方法并使用global关键字来确保您使用这些全局变量。

def whowon(MyChoice, aiChoice):
    global aiWins
    global MyWins
    global found

    print (MyChoice)
    print (aiChoice)  
    # etc... 

然后,并不真正需要那些返回语句。此外,任何功能都会在第一个返回语句结束。

最后,input()返回一个字符串,因此你的if语句将整数与字符串进行比较,这是假的,因此它们按预期执行。

要解决此问题,您需要在比较之前将输入转换为整数。您可以直接在输入法

上执行此操作
MyChoice = int(input("> "))

或直接参数

whowon(int(MyChoice), aiChoice)

或者对函数中if语句中的每个变量。由您决定

相关问题