-1

I am trying to fix a Text RPG I made and I just need to know how to get a value out of an error.

I need to do this so I can make a system that allows you to put in a string and output some data for the game.

if VauleError == True:
    retry()

So what I'm using it for is this.

while do == "" or do.len(4):                            
    cls()                                                     
    do = input("Data? (Press enter if nothing.)")             
    if do == "":                                              
        while " " in name:                                    
            name = input("Name the hero. ")                   
            if " " in name:                                   
                input("Please don't use spaces")              
                cls()                                         
    else:                                                     
        try:                                                  
            name = str(do.split()[0])                         
            kills = int(do.split()[1])                        
            extra_hp = int(do.split()[2])                     
            extra_dmg = int(do.split()[3])                    
            gold = int(do.split()[4])                         
        except ValueError:                                    
            input("Invalid Save.")                            
            cls()                                             
        else:                                                 
            input("Save Loaded.") 

I don't know how i would do it. The cls() is clear screen.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
SollyBunny
  • 800
  • 1
  • 8
  • 15

1 Answers1

1

Edited according to comment.

Use a try and except block.

user_input = ""
while user_input == ""
    try:
        user_text = input()
        #Code that throws the value error
    except ValueError:
        print("Invalid option. Please input...")
        user_text = ""
        #What you want to happen for this specific error
#Continue with program

This will make the user_text go to "" again every time the ValueError gets thrown, keeping the loop going until ValueError doesn't get thrown.

Davy M
  • 1,697
  • 4
  • 20
  • 27