I get a crash whenever I try to input a string instead of a number in Q1. How can I solve this?
this is my solution but I want to avoid the crash whenever the user inputs a string instead of an integer
num = int(input("Please input a number beween 1 and 5 \n\n >>>>>"))
if num == 1:
print("One")
elif num == 2:
print("Two")
elif num == 3:
print("three")
elif num == 4:
print("four")
elif num == 5:
print("Five")
else: print("Wrong Input Please Enter a number between 1 and 5")
6 answers ( 0 marked as helpful)
Thanks, I got the answer after solving the 3rd question
num = input("Please input a number beween 1 and 5 \n\n >>>>>")
if num.isdigit():
if num == "1":
print("One")
elif num == "2":
print("Two")
elif num == "3":
print("Three")
elif num == "4":
print("Four")
elif num == "5":
print("Five")
else: print("Wrong Input Please Write a number between 1 and 5")
else: print("Please write your selected number in digits only")
My answer for the 3rd question I searched how to generate a random number in python to get the code I wanted
import random
num = random.randint(0, 10)
guess = input('Guess the number between 1-10:> ')
if guess.isdigit():
guess = int(guess)
if guess == num:
print('You guessed the correct number! You win!')
elif guess > num and guess <= 10:
print('You guessed too high. Sorry you lose! The correct answer is ',num)
elif guess < num and guess >= 1:
print('You guessed too low. Sorry you lose! The correct answer is ',num)
else:
print('Out of range')
else:
print('That\'s not even an integer! What are you playing at?!')
My answer for the 4th question:
name = input("Please enter your name \n >>>>")
length = len(name)
if name.isalpha():
name = name.lower
if length > 5:
print("your name is ",length," characters long")
else: print("The length of your name is a secert")
else: print("That's not even a name")
My answer for the 5th question:
num1 = input("Please enter a number between 1 and 20 \n >>>>")
num2 = input("Please enter another number between 1 and 20 \n >>>>")
if num1.isdigit() and num2.isdigit():
num1 = int(num1)
num2 = int(num2)
if num1 and num2 >= 1 and num1 and num2 <= 20:
if num1 > 15 and num2 > 15:
print("Your numbers product is ", num1 * num2)
elif num1 > 15 or num2 > 15:
print("Your numbers sum is ", num1 + num2)
else:print("ZERO")
else: print("One or both of your entered numbers is out of range please try again")
else:print("One or both of your entered vlaues is not even a number please try again")
My answer for the 6th question:
num1 = input("Please enter a number \n >>>>")
num2 = input("Please enter another number \n >>>>")
if num1.isdigit() and num2.isdigit():
num1 = int(num1)
num2 = int(num2)
print("Before swapping first number =",num1 ,"and your second number =",num2)
num1,num2 = num2,num1
print("After swapping first number =",num1 ,"and your second number =",num2)
else:print("One or both of your entered vlaues is not even a number please try again")
Even my spyder too crashes after every input statement soo I changed my IDE from Spyder to Pycharm If you also switch IDE and need a tutorial then might watch Chapter 22 of this Course. Don't know whats wrong with Spyder soo better to change it