ValueError:以10为基数的int()无效。

时间:2021-02-10 22:32:17

I made a program where the user enters a number, and the program would count up to that number and display how much time it took. However, whenever I enter letters or decimals (i.e. 0.5), I would get a error. Here is the full error message:

我做了一个程序,用户输入一个数字,程序就会数到那个数字,并显示它花了多少时间。然而,每当我输入字母或小数(即0.5)时,我就会得到一个错误。以下是完整的错误信息:

Traceback (most recent call last):
  File "C:\Documents and Settings\Username\Desktop\test6.py", line 5, in <module>
    z = int(z)
ValueError: invalid literal for int() with base 10: 'df'

What can I do to fix this?

我能做些什么来解决这个问题?

Here is the full code:

这是完整的代码:

import time
x = 0
print("This program will count up to a number you choose.")
z = input("Enter a number.\n")
z = int(z)
start_time = time.time()
while x < z:
    x = x + 1
    print(x)
end_time = time.time()
diff = end_time - start_time
print("That took",(diff),"seconds.")

Please help!

请帮助!

2 个解决方案

#1


8  

Well, there really a way to 'fix' this, it is behaving as expected -- you can't case a letter to an int, that doesn't really make sense. Your best bet (and this is a pythonic way of doing things), is to simply write a function with a try... except block:

嗯,确实有一种方法可以“修复”这个,它的行为和预期一样——你不能把字母写在int上,这是没有意义的。你最好的方法(这是一种python的做事方法),就是简单地用一个尝试写一个函数…除了块:

def get_user_number():
    i = input("Enter a number.\n")
    try:
        # This will return the equivalent of calling 'int' directly, but it
        # will also allow for floats.
        return int(float(i)) 
    except:
        #Tell the user that something went wrong
        print("I didn't recognize {0} as a number".format(i))
        #recursion until you get a real number
        return get_user_number()

You would then replace these lines:

然后你将替换这些线:

z = input("Enter a number.\n")
z = int(z)

with

z = get_user_number()

#2


1  

Try checking

试着检查

if string.isdigit(z):

And then executing the rest of the code if it is a digit.

然后执行剩下的代码,如果它是一个数字。

Because you count up in intervals of one, staying with int() should be good, as you don't need a decimal.

因为你每隔一段时间就数一遍,用int()应该是好的,因为你不需要一个小数。

EDIT: If you'd like to catch an exception instead as wooble suggests below, here's the code for that:

编辑:如果您想要捕获一个异常,而不是像wooble所建议的那样,下面是代码:

try: 
    int(z)
    do something
except ValueError:
    do something else

#1


8  

Well, there really a way to 'fix' this, it is behaving as expected -- you can't case a letter to an int, that doesn't really make sense. Your best bet (and this is a pythonic way of doing things), is to simply write a function with a try... except block:

嗯,确实有一种方法可以“修复”这个,它的行为和预期一样——你不能把字母写在int上,这是没有意义的。你最好的方法(这是一种python的做事方法),就是简单地用一个尝试写一个函数…除了块:

def get_user_number():
    i = input("Enter a number.\n")
    try:
        # This will return the equivalent of calling 'int' directly, but it
        # will also allow for floats.
        return int(float(i)) 
    except:
        #Tell the user that something went wrong
        print("I didn't recognize {0} as a number".format(i))
        #recursion until you get a real number
        return get_user_number()

You would then replace these lines:

然后你将替换这些线:

z = input("Enter a number.\n")
z = int(z)

with

z = get_user_number()

#2


1  

Try checking

试着检查

if string.isdigit(z):

And then executing the rest of the code if it is a digit.

然后执行剩下的代码,如果它是一个数字。

Because you count up in intervals of one, staying with int() should be good, as you don't need a decimal.

因为你每隔一段时间就数一遍,用int()应该是好的,因为你不需要一个小数。

EDIT: If you'd like to catch an exception instead as wooble suggests below, here's the code for that:

编辑:如果您想要捕获一个异常,而不是像wooble所建议的那样,下面是代码:

try: 
    int(z)
    do something
except ValueError:
    do something else