elif语句每次都错误地返回“丢失”的结果。 (家庭作业)

时间:2011-07-03 19:59:51

标签: python

我已根据所做的更改更新了我的代码。我的结果仍然不正确......

# Import statements
import random

# Define main function that will ask for input, generate computer choice,
# determine winner and show output when finished.

def main():
    # Initialize Accumulators
    tie = 0
    win = 0
    lose = 0
    score = 0
    # initialize variables

    user = 0
    computer = 0

    # Initialize loop control variable
    again = 'y'
    while again == 'y':
        userInput()
        computerInput()
        if score == win:
            print('You won this round, good job!')
            win += 1
        elif score == tie:
            print('You tied this round, please try again!')
            tie += 1
        else:
            print('You lost this round, please try again!')
            lose += 1

        again = input('Would you like to play another round (y/n)? ')
    #determine winning average
    average = (win / (win + lose + tie))
    print('You won ', win, 'games against the computer!')
    print('You lost ', lose, 'games against the computer.')
    print('You tied with the computer for', tie)
    print('Your winning average is', average)
    print('Thanks for playing!!')

# get user input for calculation
def userInput():
    print('Welcome to Rock, Paper, Scissor!')
    print('Please make your selection and and Good Luck!')
    print('1) Rock')
    print('2) Paper')
    print('3) Scissor')
    user = int(input('Please enter your selection here: '))
    print('You selected', user)

# get compter input for calculation
def computerInput():
    computer = random.randint(1, 3)
    print('The computer chose', computer)

def getScore():
    if user == 1 and computer == 3:
        score = win
        return score
    elif user == 2 and computer == 1:
        score = win
        return score
    elif user == 3 and computer == 2:
        score = win
        return score

    elif user == computer:
        score = tie
        return score
    else:
        score = lose
        return score


# Call Main

main()

3 个答案:

答案 0 :(得分:4)

在Python中:

>>> print("3" == 3)
False

字符串和整数是不同数据类型的值,并且不会比较相等。尝试将输入更改为:

userInput = int(input('Please enter your selection here: '))

这会将用户输入的字符串转换为数字,以便以后进行比较。 (请注意,我假设您使用的是Python 3.x,因为input()在Python 2.x中的行为略有不同。)

请注意,如果键入除数字以外的任何内容,则会引发错误。

更新:正如@FelipeFG在下面的评论中指出的那样,您还使用值功能 userInput >由用户键入。您需要更改其中一个的名称,例如:

def getUserInput():
    ...

另外不要忘记更改调用该功能的位置。对computerInput执行相同操作(更改为getComputerInput)。

稍后,您可以将这些更改为返回值的实际功能。

答案 1 :(得分:2)

  1. userInput()调用函数“userInput”,但您放弃了结果。同样的说法适用于computerInput()

  2. userInput == 1询问函数 userInput本身是否等于1.事实并非如此。同样的说法适用于computerInput == 3和其他人。

  3. 在函数“userInput”中,userInput = ...将名称“userInput”绑定到表达式的结果。这使得“userInput”成为函数的局部变量。该函数不会显式返回任何内容,因此返回None

  4. 如果您使用的是Python 3,input会返回一个字符串,您应该将其结果转换为int。如果您使用的是Python 2,input会评估输入的内容,这是不安全的;您应该使用raw_input代替并将其结果转换为int。

答案 2 :(得分:1)

您需要与函数的返回值进行比较,而不是函数本身。

此外:

again = input('Would you like to play another round (y/n)? ')

如果输入yn,这将抛出异常,因为没有该名称的已定义标识符!您想要使用的是raw_input()

编辑:正如Greg所指出的,这仅适用于Python 2.x.你好像在使用Python3。