我已经完成了这段代码:
Num = int(input("Enter a large number: "))
Num2 = Num+10
print("If I added 10 to your number it would be:" , Num2)
print("The new number times 2 would be " , Num2*2)
count = 0
number = Num
while (number > 1):
number = number//10
count = count + 1
i = 1
for i in range(1,count+1):
Num = Num+1*(10**i)
i = i+1
print("adding one to every digit on your first number would be: " , Num + 1)
在此代码中,如果用户输入的值是整数9,我想结束它。 我不知道怎么 请帮助
如果输入9,则代码应刚刚结束,并且输出9无效
答案 0 :(得分:1)
检查用户输入的数字是否为9
并退出并显示一条消息。
Num = int(input("Enter a large number: "))
if Num == 9:
print("Invalid Input")
exit()
else:
Num2 = Num + 10
print("If I added 10 to your number it would be:", Num2)
print("The new number times 2 would be ", Num2 * 2)
count = 0
number = Num
while (number > 1):
number = number // 10
count = count + 1
i = 1
for i in range(1, count + 1):
Num = Num + 1 * (10 ** i)
i = i + 1
print("adding one to every digit on your first number would be: ", Num + 1)
或如果要检查输入中是否有9
。
Num = str(input("Enter a large number: "))
if "9" in Num:
print("Invalid Inputs")
exit()
答案 1 :(得分:0)
检查Num = 9
是否执行os.exit(1)
Num = int(input("Enter a large number: "))
if Num == 9:
exit()
Num2 = Num+10
print("If I added 10 to your number it would be:" , Num2)
print("The new number times 2 would be " , Num2*2)
count = 0
number = Num
while (number > 1):
number = number//10
count = count + 1
i = 1
for i in range(1,count+1):
Num = Num+1*(10**i)
i = i+1
print("adding one to every digit on your first number would be: " , Num + 1)
答案 2 :(得分:0)
要排除所有包含数字9的值,请在将其转换为整数之前将其检查为字符串:
num_str = str(input("Enter a large number: "))
if "9" in num_str:
print("Value contains a 9. This is invalid")
exit()
num = int(num_str)
...
要仅排除值9,请检查该特定值:
num = int(input("Enter a large number: "))
if num == 9:
print("Value is 9. This is invalid")
exit()
...
答案 3 :(得分:-1)
只需执行
Num = int(input("Enter a large number: "))
if Num==9:
print("9 is incorrect")
else:
Num2 = Num+10
print("If I added 10 to your number it would be:" , Num2)
print("The new number times 2 would be " , Num2*2)
count = 0
number = Num
while (number > 1):
number = number//10
count = count + 1
i = 1
for i in range(1,count+1):
Num = Num+1*(10**i)
i = i+1
print("adding one to every digit on your first number would be: " , Num + 1)
希望有帮助