【发布时间】:2015-02-14 05:55:11
【问题描述】:
我大约 5 周前才开始编写代码,我正在为我的游戏开发课程编写代码,但我非常卡在一个问题上。当我运行我的代码时,我得到“ValueError: invalid literal for int() with base 10: ''”错误,我不确定我做错了什么。我将在下面发布我的代码。任何帮助表示赞赏。谢谢!
start = 0
def addition(num1):
return num1 + num1
def square(num):
print("I'm in square")
return num * num
def display(message):
"""Display game instuctions"""
print(message)
def instructions():
"""Display game instuctions"""
print("Welcome to the world's greatest game")
def main():
instructions()
scores = [("Roger", 3456), ("Justin", 2320), ("Beth", 1422)]
start = input("Would you like to view the high score options? y/n ")
if start == "y":
print("""\
Hello! Welcome to the high scores!
Here are the current high score leaders!:
""")
print(scores)
print("""\n\
0 - Sort high scores
1 - Add high score
2 - Reverse the order
3 - Remove a score
4 - Square a number
5 - Add 2 numbers together
""")
option = int(input())
while option < 6:
start = int(input("Please enter your selection"))
print(scores)
if option == 0:
scores.sort()
print("These are the scores sorted alphabetically")
print(scores)
if option == 1:
print(scores)
print("Please enter your name and score; After entering your name, hit the return key and enter your score")
name = input()
score = int(input())
entry = (name,score)
scores.append(entry)
print(scores)
if option == 2:
print(scores)
scores.reverse()
print("\nHere are the scores reversed")
print(scores)
if option == 3:
print(scores)
print("Please enter the high score you would like to remove. After typing the name, hit the return key and enter the score")
name1 = input()
score1 = int(input())
remove = (name1,score1)
scores.remove(remove)
print(scores)
if option == 4:
val = int(input("Give me a number to square"))
sqd = square(val)
print(sqd)
if option == 5:
val0 = int(input("Give me one number"))
val1 = int(input("Give me another number"))
addi = (val0 + val1)
print(addi)
main()
【问题讨论】:
-
您必须输入字符串值并尝试将字符串转换为整数。您在哪一行遇到异常,用户对该语句的输入是什么?
-
您正在尝试将空字符串转换为
int。检查您的input电话。
标签: python function loops python-3.x base