为什么我在python字符串输入中遇到问题? (名称未定义)

时间:2016-12-11 02:35:01

标签: python python-2.7

我正在学习Python,我正在尝试创建一个足球模拟器,当我输入一个团队名称时,它会给我一个name '' not defined error。我用python 2.7在终端上编码:

import time
import random
print("Welcome to The Football Simulator v1.0!")
team1 = input('Enter the first team ')
team2 = input('Enter the second team ')
venue = input('Where are they playing ')
print('Simulating...')
time.sleep(5)
def determineScore() :
    x = random.randint(1,10)
    if x <= 9:
            y = random.randint(1,6)
            if y <= 2:
                    score = 0
            elif y <= 4:
                    score = 1
            elif y == 5:
                    score = 2
            elif y == 6:
                    score = 3
    elif x == 10:
            score = random.randint(1,5)
    return score
team1Score = determineScore()
team2Score = determineScore()

print("==============RESULT==============")
print(team1, '                         ',team1Score)
print(team2, '                         ',team2Score)

现在我将告诉你错误:

Welcome to The Football Simulator v1.0!
Enter the first team: barcelona
Traceback (most recent call last):
  File "footballsimulator.py", line 4, in <module>
    team1 = input("Enter the first team: ")
  File "<string>", line 1, in <module>
NameError: name 'barcelona' is not defined.

另外,如果我给它一个带空格的名称,它会显示unexpected EOF。 奇怪的是,它适用于数字。

如果我犯了一个蛋头错误,请原谅我,因为我刚刚开始python而且我只有13岁。

1 个答案:

答案 0 :(得分:0)

input评估输入,并输入任何字符串引发错误。请改用raw_input

相关问题