将用户输入的整数限制为 8 位
我有一个信用卡验证小程序,用户必须输入 8 位整数。该程序工作正常,我唯一的问题似乎是将用户输入限制为 8 位数字。
prompt = "Please, enter the 8 digit number of your card: "
while True:
try:
userinput = int(raw_input(prompt))
except ValueError:
print('The gematric value must be an 8 digit integer. Try again')
else:
break
userinput = str(userinput)
a = int(userinput[7])+int(userinput[5])+int(userinput[3])+int(userinput[1])
b1 = str(int(userinput[6])*20)
b2 = str(int(userinput[4])*20)
b3 = str(int(userinput[2])*20)
b4 = str(int(userinput[0])*20)
y = int(b1[0])+int(b1[1])+int(b2[0])+int(b2[1])+int(b3[0])+int(b3[1])+int(b4[0])+int(b4[1])
x = (a+y)
if x % 10 == 0:
print('The card number you entered is valid!')
else:
print('The card number you entered is invalid!')
我知道 len() 不适用于整数。那么我如何将 8 位数字限制合并到我的 while true 循环中?
任何帮助将不胜感激。
I have this little credit card validation program where the user has to input an 8 digit integer. The program works fine, my only problem seems to be limiting user input to 8 digits only.
prompt = "Please, enter the 8 digit number of your card: "
while True:
try:
userinput = int(raw_input(prompt))
except ValueError:
print('The gematric value must be an 8 digit integer. Try again')
else:
break
userinput = str(userinput)
a = int(userinput[7])+int(userinput[5])+int(userinput[3])+int(userinput[1])
b1 = str(int(userinput[6])*20)
b2 = str(int(userinput[4])*20)
b3 = str(int(userinput[2])*20)
b4 = str(int(userinput[0])*20)
y = int(b1[0])+int(b1[1])+int(b2[0])+int(b2[1])+int(b3[0])+int(b3[1])+int(b4[0])+int(b4[1])
x = (a+y)
if x % 10 == 0:
print('The card number you entered is valid!')
else:
print('The card number you entered is invalid!')
I know len() does not work with integers. So how would i incorporate that 8 digit limit into my while true loop?
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下操作。
Try the following.