我得到" ValueError:对于带有基数为10的int()的无效文字:''"'错误。 我的规格:
我的代码
import random
length = input("Input the length of the random list: ")
temp = input("Input the range of values for the random list (0-n)")
def gen_list(L, T):
LIST = []
ranlower = T[0:T.index('-')]
ranhigher = T[T.index('-')+1:len(T)]
for num in range(L):
x = random.randint(int(ranlower),int(ranhigher))
LIST.append(x)
tempmax = ranlower
tempmin = ranhigher
for i in range (L):
for t in range (L):
if LIST[t] > tempmax:
tempmax = LIST[t]
if LIST[t] < tempmin:
tempmin = LIST[t]
print("Unsorted List: ", LIST)
print("The largest value in the list was: ", tempmax, " with upper bound at ", ranhigher, ".")
print("The smallest value in the list was: ", tempmin, " with lower bound at ", ranlower, ".")
print("The random unsorted list has ", L, " items.")
sort_numbers(LIST)
def sort_numbers(s):
for i in range(1, len(s)):
# let's see what values i takes on
print ("i = ", i)
val = s[i]
j = i - 1
while (j >= 0) and (s[j] > val):
s[j+1] = s[j]
j = j - 1
print (s)
s[j+1] = val
print(s)
gen_list(length, temp)
这是完整的追溯:
Input the length of the random list: 10
Input the range of values for the random list (0-n)
10-15
Traceback (most recent call last):
File "/private/var/folders/8_/7j1ywj_93vz2626l_f082s9c0000gn/T/Cleanup At Startup/Program 1-437933490.889.py", line 42, in <module>
gen_list(length, temp)
File "/private/var/folders/8_/7j1ywj_93vz2626l_f082s9c0000gn/T/Cleanup At Startup/Program 1-437933490.889.py", line 13, in gen_list
x = random.randint(int(ranlower),int(ranhigher))
ValueError: invalid literal for int() with base 10: ''
答案 0 :(得分:1)
正如您从错误消息中看到的那样,您尝试在空字符串int()
上调用''
。
由于产生错误的行包含对int()
的两次调用,这意味着ranlower
或ranhigher
是空字符串。找出原因(print
语句对此有帮助),您的问题将得到解决。