Python 你如何防止这种情况;ValueError:以10为基数的int()的文本无效:'';,使用定义的函数?
我在猜数字游戏。我试着让用户得到3次猜测,如果猜测太大,代码会说猜测太大,如果猜测太小,那么代码应该告诉他们,同时允许他们重新输入猜测,如果他们的答案超出了1-10的范围,因为这是随机生成的数字的来源。我计划增加其他级别和领导委员会,所以如果有人能帮助我,我将不胜感激。这是我的密码:Python 你如何防止这种情况;ValueError:以10为基数的int()的文本无效:'';,使用定义的函数?,python,Python,我在猜数字游戏。我试着让用户得到3次猜测,如果猜测太大,代码会说猜测太大,如果猜测太小,那么代码应该告诉他们,同时允许他们重新输入猜测,如果他们的答案超出了1-10的范围,因为这是随机生成的数字的来源。我计划增加其他级别和领导委员会,所以如果有人能帮助我,我将不胜感激。这是我的密码: import time import random answer1= "" answer2= "" answer3= "" one= "You have two more guesses: " two= "Yo
import time
import random
answer1= ""
answer2= ""
answer3= ""
one= "You have two more guesses: "
two= "You only have one more guess. Be careful: "
number= random.randint(1,10)
number=str(number)
def code(a,number,b,n,d,e):
a = int(a)
number= int(number)
if a > number and a <11:
b=input("the number is too big. "+ n )
elif a< number and a>-1:
d= input("the number is too big. "+ n )
elif a> 10 or a<0:
e=input("please input a number smaller than 10 and bigger than 0: ")
print(" -------------------- Guess the number game --------------------")
time.sleep(1)
answer= input("The aim of the game is to guess the number. Are you ready? (yes/no): ").lower()
if "yes" in answer:
answer1=input("I am thinking of a number, between 0 and 10, What is the number: ")
if answer1 == number:
print ("Correct! Wow.. it only took you one guess")
time.sleep(1)
print("Come back again sometime...")
time.sleep(1)
print("BYE!")
time.sleep(1)
quit()
elif answer1 != number:
code(answer1,number,answer2,one,answer2,answer2)
if answer2 == number:
print ("Correct! it took you two turns.")
time.sleep(1)
print("Come back again sometime")
time.sleep(1)
print("BYE!")
time.sleep(1)
quit()
elif answer2 != number:
code(answer2,number,answer3,two,answer3,answer3)
if answer3 == number:
print ("Correct! Phew.... you guessed correctly in your last guess!")
time.sleep(1)
print("Come back again sometime")
time.sleep(1)
print("BYE!")
time.sleep(1)
quit()
elif answer3 != number:
print("Incorrect..... Sorry! ")
time.sleep(1)
print("The correct number was "+number)
time.sleep(1)
print("You lose! But don't stop trying!")
time.sleep(1)
quit()
elif "yes" != answer:
print("That's too bad...")
time.sleep(1)
print("Come back when you're ready to....")
time.sleep(2)
print("LOSE!!")
time.sleep(1)
quit()
导入时间
随机输入
回答1=“”
回答2=“”
回答3=“”
one=“您还有两个猜测:”
two=“你只能再猜一次。小心:”
number=random.randint(1,10)
编号=str(编号)
def代码(a、编号、b、n、d、e):
a=int(a)
数字=整数(数字)
如果a>数字和a-1:
d=输入(“数字太大。”+n)
elif a>10或a分析
您正在尝试将空字符串转换为整数。繁荣
如果您真的想要这样的功能,请将所有三个猜测初始化为“-1”之类的值,这样这些值在语法上是合法的,但在语义上超出了范围
改进的检查
if a.isdigit():
a = int(a)
else:
print ("Don't be silly! I need a number!")
建议
测试前不要写太多代码。写几行,测试一下,直到他们做了你想做的事情才继续。在这里,您试图在85行代码中找到一个小错误
了解函数如何工作。您的code
函数接受六个参数,但忽略b
、d
和e
,覆盖输入的任何值。我想你可能已经把这些和“可变”的价值观混淆了,你可以改变的东西。调用程序将不会接收指定的值
使用有意义的变量名。分析
您正在尝试将空字符串转换为整数。繁荣
如果您真的想要这样的功能,请将所有三个猜测初始化为“-1”之类的值,这样这些值在语法上是合法的,但在语义上超出了范围
改进的检查
if a.isdigit():
a = int(a)
else:
print ("Don't be silly! I need a number!")
建议
测试前不要写太多代码。写几行,测试一下,直到他们做了你想做的事情才继续。在这里,您试图在85行代码中找到一个小错误
了解函数如何工作。您的code
函数接受六个参数,但忽略b
、d
和e
,覆盖输入的任何值。我想你可能已经把这些和“可变”的价值观混淆了,你可以改变的东西。调用程序将不会接收指定的值
使用有意义的变量名。您的问题是python没有像您认为的那样传递变量
def code(a,number,b,n,d,e):
a = int(a)
number= int(number)
if a > number and a <11:
b=input("the number is too big. "+ n )
elif a< number and a>-1:
d= input("the number is too big. "+ n )
elif a> 10 or a<0:
e=input("please input a number smaller than 10 and bigger than 0: ")
然后通过以下方式调用:
answer2 = code(answer1, number, one)
见
一些进一步的说明:
- 正如其他人所建议的,您可能需要添加输入错误检查
- 你说“数字太大了”,无论是对太大还是太小的猜测
- 您可能希望查看循环,以便不重新启动即可继续播放
- 我推荐一个关于函数和传递变量的教程,因为这是根本问题
您的问题是python并不像您想象的那样传递变量
def code(a,number,b,n,d,e):
a = int(a)
number= int(number)
if a > number and a <11:
b=input("the number is too big. "+ n )
elif a< number and a>-1:
d= input("the number is too big. "+ n )
elif a> 10 or a<0:
e=input("please input a number smaller than 10 and bigger than 0: ")
然后通过以下方式调用:
answer2 = code(answer1, number, one)
见
一些进一步的说明:
- 正如其他人所建议的,您可能需要添加输入错误检查
- 你说“数字太大了”,无论是对太大还是太小的猜测
- 您可能希望查看循环,以便不重新启动即可继续播放
- 我推荐一个关于函数和传递变量的教程,因为这是根本问题
您可以捕获异常:
try:
a = int(a)
exception ValueError as error:
print(error)
print('"{}" is not an integer'.format(a))
您可以捕获异常:
try:
a = int(a)
exception ValueError as error:
print(error)
print('"{}" is not an integer'.format(a))
您可能想尝试一种非常pythonical的方法来检查变量,方法是使用try。。。施工除外。因此,a=int(a)类似于这样:
try:
a = int(a)
except:
print ... something ...
正如@TemporalWolf指出的,函数有一个大问题。看一看。
最后,您尝试获取数字并检查它,因此您可能需要一个循环,直到用户给出所需范围内的数字。看起来像这样
a=-1
while 0<a<10:
answer=input("I am thinking of a number, between 0 and 10, What is the number: ")
try:
a = int(answer)
except:
print "I need a number!"
a = -1
continue
else:
if a<1:
print "number is negative"
continue
if a>10:
print "number too big"
continue
a=-1
当0时,您可能想尝试一种非常类似于pythonical的方法,通过使用try。。。施工除外。因此,a=int(a)类似于这样:
try:
a = int(a)
except:
print ... something ...
正如@TemporalWolf指出的,函数有一个大问题。看一看。
最后,您尝试获取数字并检查它,因此您可能需要一个循环,直到用户给出所需范围内的数字。看起来像这样
a=-1
while 0<a<10:
answer=input("I am thinking of a number, between 0 and 10, What is the number: ")
try:
a = int(answer)
except:
print "I need a number!"
a = -1
continue
else:
if a<1:
print "number is negative"
continue
if a>10:
print "number too big"
continue
a=-1
而0当回答1时!=数字,这是预期的answer2
在顶部被设置为”
,并且从不重新分配。如果其他0
则可以尝试a=int(a),因此空白输入被转换为0。您应该调查循环(例如while
,for
),这将大大简化您的代码。当answer1!=数字,这是预期的answer2
在顶部设置为”
,并且从不重新分配。如果其他0
则可以尝试a=int(a),因此空白输入被转换为0。您应该调查循环(例如while
,for
),这将大大简化您的代码。似乎@User9123询问了一些不同的问题,不是吗?这是他问题的根源。他可能还想为非整数用户输入添加错误处理,但这将修复黄金路径。只添加整数错误处理不会修复他的程序。同意,但我们应该调试代码还是回答问题?@rth如果有人问一个问题,我倾向于认为你应该回答实际问题,而不是偏离基准的问题(或者在本例中是更深层次问题的症状)。解决程序,以便我可以在es中对其进行反向工程