I'm a beginner in the language python and have occurred some issues writing code containing a while loop which i want to check multiple conditions with one of the conditions not being checked and issues with strings/integers.
The original code (2nd code sample) was what I first wrote but the issue with it was that after a key met the condition of the first loop it would move on to the second loop so that if i entered nothing into the program when its running it will just come up with an error instead of please enter a key. The only way i could think of to fix this was to alter the code so it checks for one of multiple conditions with one while loop. But i had some issues with the variable key for each condition being a different type (integer/string) and it wasn't fixed when i attempted to use int() and str(). Besides that the code doesn't seem to check for all of the conditions. A major drawback of the altered code is that it will no longer tell the user what exactly they did wrong e.g. didn't enter anything, entered a character that's not a number
The Code:
def Key(key):
while len(key) < 1 or key.isalpha() == True or ((key > 25) or (key < -25)):
print("The key must be a number between numbers 26 and -26")
key = int(input("What is the key: "))
print (key)
key = input("What is the key: ")
Key(key)
The Original Code:
def Key(key):
while len(key) < 1:
print("Please enter a key")
key = int(input("What is the key: "))
while key.isalpha() == True :
print("Please enter a number!")
key = int(input("What is the key: "))
key=int(key)
while (key > 25) or (key < -25):
print("The key must be in between numbers 26 and -26")
key = int(input("What is the key: "))
print key
key = input("What is the key: ")
Key(key)
Any help, improvements and better way of doing this would be extremely helpful Sorry for the ridiculously long post Thankyou