我希望print语句打印上下限之间的数字。
我不断收到错误代码:
Traceback (most recent call last):File "python", line 5, in <module> ValueError: non-integer arg 1 for randrange()
从程序中:
from random import*
lowRange = input('What is the lower range number?')
hiRange = input('What is the higher range nunmber?')
ran = randrange (lowRange,hiRange)
print (ran)
答案 0 :(得分:2)
nextClikcedHandle(continuationToken: string): void {
this.customerService.searchCustomersPaged(this.customerService.searchTerm, this.currentContinuationToken)
.subscribe(resp => {
//add current continuation token, to previous now, as this will be used for 'previous' searching
this.previousContinuationTokens.push(this.currentContinuationToken);
//set next continuation token received by server
this.currentContinuationToken = resp.headers.get('continuationToken');
//return search results
this.customerService.searchResults.next(resp.body);
});
函数始终返回一个字符串。如果要使用整数(在这种情况下),则必须使用input()
将这些字符串转换为整数。但是,如果用户输入的内容无法转换为整数(例如“ hi”,或仅按回车键),则尝试转换时会收到错误消息。为了解决这个问题,您将需要研究try和except语句。希望有帮助!
答案 1 :(得分:0)
尝试一下:
在这里,直到您输入数字,它都不会停止。 int
以外的其他输入将被视为无效输入。
代码中的问题是从输入中读取的所有内容都被当作字符串。
from random import*
while True:
try:
lowRange = int(input('What is the lower range number?'))
break
except:
print("That's not a valid input!")
while True:
try:
hiRange = int(input('What is the higher range nunmber?'))
break
except:
print("That's not a valid input!")
ran = randrange (lowRange,hiRange)
print (ran)