-2

Let's get straight to the problem: when i run the code, and type letters instead of numbers to the first input my python gets an error. How do i make my python know that if someone types in letters instead of numbers should get warned and repeat the code? Im trying to fix it from about two hours.

Thank you for help

Also sorry for my really bad english

import time
import random

def repeatt():
    od = int(input("Wpisz do ktorej liczby liczba ma byc losowana: "))
    doo = int(input("Do ktorej: "))
    if od >= doo:
        print("Jeszcze raz :")
        repeatt()
    elif od <= doo:
        wylosowana = random.randint(od, doo)
        print("Wylosowana liczba: ", wylosowana)

print("Witaj! Od czego chcialbys zaczac?:")

print(
     """
     1. forin slowo
     2. oblicz ile ja zyje
     3. oblicz, ile mam zaplacic
     4. tekst
     5. losowanie liczby
     """
)

choice = int(input("Wpisz liczbe: "))

if choice == 1:
    slowo = input("Wprowadz slowo: ")
    for letter in slowo:
        print(letter)
elif choice == 2:
    obliczanie = int(input("Wprowadz, ile masz lat: "))
    oblicz = obliczanie * 60 * 60
    print("Zyjesz juz ponad ", obliczanie * 60 * 60, "sekund")
elif choice == 3:
    pieniadze = int(input("Ile podczas miesiacu zarabiasz?: "))
    print("Na jedzenie: ", pieniadze / 5)
elif choice == 4:
    wiadomosc = input("Wpisz jakąs wiadomosc: ")
    def repeat():
        wybor = input("upper, lower, title?: ")
        if wybor == "upper":
            print(wiadomosc.upper())
        elif wybor == "lower":
            print(wiadomosc.lower())
        elif wybor == "title":
            print(wiadomosc.title())
        else:
            print("Wpisz upper, lower lub title")
    wybor = input("upper, lower, title?: ")
    if wybor == "upper":
        print(wiadomosc.upper())
    elif wybor == "lower":
        print(wiadomosc.lower())
    elif wybor == "title":
        print(wiadomosc.title())
    else:
        print("Wpisz proprawnie")
        repeat()

elif choice == 5:
    od = int(input("Wpisz liczbe od ktorej ma byc losowana: "))
    doo = int(input("Do ktorej: "))
    if od >= doo:
        print("Jeszcze raz :")
        repeatt()
    elif od <= doo:
        wylosowana = random.randint(od, doo)
        print("Wylosowana liczba: ", wylosowana)
    else:
        print("Tylko liczby")

else:
    print("Wpisz liczbe od 1 do 3")
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895

3 Answers3

1
choice = int(input("Wpisz liczbe: "))

Replace the above line with below code

While True:
    choice = input("Wpisz liczbe: ")
    if choice.isdigit():
        choice = int(choice)
        # your code of if conditions
    else:
        print("please enter valid input")
        continue
newbie
  • 1,282
  • 3
  • 20
  • 43
0
while True:
    try:
        choice = int(input("Wpisz liczbe: "))
        break
    except ValueError:
        print("No letters allowed, please try again")
Martinez
  • 149
  • 2
  • 12
0
def repeatt():
    def redo(): #Repeating function
        try:
            od = int(input("Wpisz do ktorej liczby liczba ma byc losowana: "))
        except: #If the user enters a string instead of int it will go back to redo() which will repeat until user enters a int.
            print("Enter A Number!")
            redo()

    redo() #Leads to the input 
repeatt()
Mike
  • 70
  • 1
  • 11