Jul-22-2021, 11:43 AM
Hello Python Forum!
I´m fairly new to coding and I have a general question to the way how I code. I tried to put the whole code into one singular while-loop and also one "try and except" statement, but the problem with this method is that once the users types in an erroneous input, he has to start all over again. So I have added to every input section a while loop and a try-except statement, so that the user who inputs the data doesn´t have to start from new again.
My question is whether there is a more elegant way, where I don´t have to always place those two elements, so that the user doesn´t have to start from the very beginning again?
Is there a command in python, where it creates a checkpoint in the code and starts from that point when a wrong input has been made?
I would also be happy for some general feedback for my code! :)
Thanks!
SW
I´m fairly new to coding and I have a general question to the way how I code. I tried to put the whole code into one singular while-loop and also one "try and except" statement, but the problem with this method is that once the users types in an erroneous input, he has to start all over again. So I have added to every input section a while loop and a try-except statement, so that the user who inputs the data doesn´t have to start from new again.
My question is whether there is a more elegant way, where I don´t have to always place those two elements, so that the user doesn´t have to start from the very beginning again?
Is there a command in python, where it creates a checkpoint in the code and starts from that point when a wrong input has been made?
I would also be happy for some general feedback for my code! :)
Thanks!
SW
def calc_R(x,y,z): z = (x*z)/y return print("Der spez. Widerstandswert lautet",z,"Ohm") x = -1 while x == -1: try: A = int(input("Geben Sie bitte den Querschnitt ein: ")) break except: print("Sie haben eine falsche Eingabe getätigt") continue while x == -1: try: l = int(input("Geben Sie bitte die Länge ein: ")) break except: print("Sie haben eine falsche Eingabe getätigt") continue while x == -1: try: auswahl = int(input("Entscheiden Sie sich zwischen Cu(1) und Alu(2): ")) except: print("Sie haben eine falsche Eingabe getätigt") continue if auswahl == 1: p = 0.0178 break elif auswahl == 2: p = 0.0245 break calc_R(p,l,A)