0
import time
import random
Score = int(0)
def crossing_river():
    print("It's the evening")
    time.sleep(2)
    print("You are standing by the bank of the river")
    time.sleep(2)
    print("There are many trees along the river on both sides")
    time.sleep(2)
    print("You must cross the river....But!!")
    time.sleep(2)
    print("There is a beast overthere...in the river...ohh")
    time.sleep(2)
    print("You have a lifejacket...small boat and a rope")
    time.sleep(1)
    print("what will You do?")
    print("1. Use the boat")
    print("2. Tie the rope between both sides and use it to cross")
    print("3. Wear the life jacket and swim")
    print("(please enter 1 or 2 or 3 )")
    Choice_1 = input()
>>>>if Choice_1 == int and 1 <= Choice_1 <= 3:
        print("Good Job...You have crossed the river successfully")
        if Choice_1 == 1 :
            print("The beast hit you")
            time.sleep(0.5)
            print("Don't worry...it's so small....not dangerous")
            Score =+ 1 
            print("Score ="+ Score)
        if Choice_1 == 2 :
            print("You haven't been hurted")
            time.sleep(1)
            print("You work smart not hard")
            Score =+ 2 
            print("Score ="+ Score)
        if Choice_1 == 3 :
            print("AWW!!..You were hit badly by the beast")
            Score =- 1 
            print("Score ="+ Score)
            time.sleep(0.5)
            print("You should take medicine")
            print("1. Take")
            print("2. Not take")
            print("(please enter 1 or 2)")
            choice_2 = input()
            if choice_2.is_integer() == True and  1 <= choice_2 <= 2:
                if choice_2 == 1:
                    Score =+ 1 
                    print(Score) 
                else:
                    Score =- 1 
                    print(Score) 
            else:
                print("foo...")
                time.sleep(1)
                print("(please enter 1 or 2)")
    else:
        print("foo...")
        time.sleep(1)
        print("(please enter 1 or 2 or 3 )")
        crossing_river()

            
crossing_river()

Doing a small text-based game....There are situations depend on user's input

What I want to do is that if user put unsuitable data, repeat the function starting from the checking if statement(mentioned with >>>> above) not from the beginning of the function

Is it possible???

I have done nested functions but it failed......also I tried to make a separated checking function ,but the code will become unclean

  • 3
    It's simple. You need to decompose your program for functions. Now you have one big linear function (in the old days sometimes they called it 'linear programming'. What you are trying to do is one of the reasons functions were invented in the first place. In other programming languages you could use GOTO statement (asm, C++, even C#). In Python you dont have such jump instruction. So use proper functional decomposition. – Pawel Jun 01 '23 at 00:33
  • In addition, in this case, a `while` loop could be used before the user input, and `break` from the loop when input is valid. – Mark Tolonen Jun 01 '23 at 00:49

0 Answers0