如何检查用户是否输入了整数 (Python)
How to check if user inputs an integer (Python)
我可能会经常用 Python 询问我的项目(因为我已经提出了 3 个帮助请求),但我只想尽可能做到最好。这次我想做一个 if
语句来检查用户是否输入了整数(数字)而不是其他东西,因为当他们不输入数字时,程序就会崩溃,我不喜欢这样,我喜欢用一条消息提示他们,说他们需要输入一个数字,别无其他。
这是我的代码:
def main():
abc = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
message = input("What's the message to encrypt/decrypt? ")
key = int(input("What number would you like for your key value? "))
choice = input("Choose: encrypt or decrypt. ")
if choice == "encrypt":
encrypt(abc, message, key)
elif choice == "decrypt":
encrypt(abc, message, key * (-1))
else:
print("Bad answer, try again.")
def encrypt(abc, message, key):
text = ""
for letter in message:
if letter in abc:
newPosition = (abc.find(letter) + key * 2) % 52
text += abc[newPosition]
else:
text += letter
print(text)
return text
main()
我猜 if
语句需要在 def encrypt(abc, message, key)
方法中的某个地方,但我可能是错的,你能帮我找出解决这个问题的方法吗,我将不胜感激你的时间来帮助我。
谢谢!!!
使用try .. except
:
try:
key = int(input('key : '))
# => success
# more code
except ValueError:
print('Enter a number only')
在您的代码中:
def main():
abc = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
message = input("What's the message to encrypt/decrypt? ")
choice = input("Choose: encrypt or decrypt. ")
def readKey():
try:
return int(input("What number would you like for your key value? "))
except ValueError:
return readKey()
key = readKey()
if choice == "encrypt":
encrypt(abc, message, key)
elif choice == "decrypt":
encrypt(abc, message, key * (-1))
else:
print("Bad answer, try again.")
def encrypt(abc, message, key):
text = ""
for letter in message:
if letter in abc:
newPosition = (abc.find(letter) + key * 2) % 52
text += abc[newPosition]
else:
text += letter
print(text)
return text
main()
我可能会经常用 Python 询问我的项目(因为我已经提出了 3 个帮助请求),但我只想尽可能做到最好。这次我想做一个 if
语句来检查用户是否输入了整数(数字)而不是其他东西,因为当他们不输入数字时,程序就会崩溃,我不喜欢这样,我喜欢用一条消息提示他们,说他们需要输入一个数字,别无其他。
这是我的代码:
def main():
abc = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
message = input("What's the message to encrypt/decrypt? ")
key = int(input("What number would you like for your key value? "))
choice = input("Choose: encrypt or decrypt. ")
if choice == "encrypt":
encrypt(abc, message, key)
elif choice == "decrypt":
encrypt(abc, message, key * (-1))
else:
print("Bad answer, try again.")
def encrypt(abc, message, key):
text = ""
for letter in message:
if letter in abc:
newPosition = (abc.find(letter) + key * 2) % 52
text += abc[newPosition]
else:
text += letter
print(text)
return text
main()
我猜 if
语句需要在 def encrypt(abc, message, key)
方法中的某个地方,但我可能是错的,你能帮我找出解决这个问题的方法吗,我将不胜感激你的时间来帮助我。
谢谢!!!
使用try .. except
:
try:
key = int(input('key : '))
# => success
# more code
except ValueError:
print('Enter a number only')
在您的代码中:
def main():
abc = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
message = input("What's the message to encrypt/decrypt? ")
choice = input("Choose: encrypt or decrypt. ")
def readKey():
try:
return int(input("What number would you like for your key value? "))
except ValueError:
return readKey()
key = readKey()
if choice == "encrypt":
encrypt(abc, message, key)
elif choice == "decrypt":
encrypt(abc, message, key * (-1))
else:
print("Bad answer, try again.")
def encrypt(abc, message, key):
text = ""
for letter in message:
if letter in abc:
newPosition = (abc.find(letter) + key * 2) % 52
text += abc[newPosition]
else:
text += letter
print(text)
return text
main()