Python Forum
Possibility to create Checkpoints in Loops?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Possibility to create Checkpoints in Loops?
#1
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


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)
Reply
#2
Whenever there is a repeated pattern in the code, you can write a function to execute this repeated pattern
def input_int(msg, errmsg):
    while True:
        try:
            return int(input(msg))
        except ValueError:
            print(errmsg)


def calc_R(x,y,z):
    z = (x*z)/y
    return print("Der spez. Widerstandswert lautet",z,"Ohm")
 
errmsg = "Sie haben eine falsche Eingabe getätigt"
A = input_int("Geben Sie bitte den Querschnitt ein: ", errmsg)
l = input_int("Geben Sie bitte die Länge ein: ", errmsg)
auswahl = input_int(
    "Entscheiden Sie sich zwischen Cu(1) und Alu(2): ", errmsg)

if auswahl == 1:
    p = 0.0178
elif auswahl == 2:
    p = 0.0245

calc_R(p,l,A)
coder_sw99 likes this post
Reply
#3
Thanks a lot, this was a big help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,406 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Need help to make an empty list with possibility to add Arnsol 1 1,804 Mar-19-2020, 06:08 PM
Last Post: michael1789

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020