-1

I am making a game of truth or dare. I cannot figure how after the else statement {the first inner else statement} I could start from the inner if statement {asking again if you want to answer yes or no, instead of truth or dare} instead of starting all the way from the beginning.

lives = 3

while lives > 0:
    choice = input("truth or dare?: ")
    time.sleep(0.5)

    if choice == "truth":
        print(random.choice(truth))
        time.sleep(0.5)
        answer_truth = input("want to answer? type yes or no: ")
        time.sleep(0.5)

        if answer_truth == "yes":
            input("> ")

            print("good answer")
            time.sleep(0.5)
            print(f"you have {lives} lives left")

        elif answer_truth == "no":
            print("you lost a life!")
            time.sleep(1)
            print(f"you have {lives} lives left")

        else:
            print("that is not an option")

    elif choice == "dare":
        print(random.choice(dare))
        time.sleep(0.5)
        do_dare = input("did you do the dare? type yes or no: ")

        if do_dare == "yes":
            print("well done!")
            time.sleep(0.5)
            print(f"you have {lives} lives left")

        elif do_dare == "no":
            print("you lost a life!")
            lives -= 1
            time.sleep(0.5)
            print(f"you have {lives} lives left")

        else:
            print("that is not an option")
    else:
        print("that is not an option")
time.sleep(0.5)
print("GAME OVER!")
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Jayden
  • 1

1 Answers1

1

You just need a loop that will only stop when the user input is valid:

truths = ["Question 1", "Question 2", "Question 3"]

# ...

if choice == "truth":
    # If there's no truth left
    if not truths:
        print("You finished all truths")
        continue

    print(random.choice(truth))
    time.sleep(0.5)
    
    # Loop indefinitely
    while True:
        answer_truth = input("want to answer? type yes or no: ").lower()
        
        # If the user answer is valid
        if answer_truth in ('yes', 'no'):
            wants_to_answer = answer_truth == 'yes'
            # Exit the loop and move to the next step
            break
        
        # If it's not valid (implicit else), continue looping
        print("that is not an option")
        
    time.sleep(0.5)

    # Check a boolean value instead of a string value
    if wants_to_answer:
        # Remove a truth from the truths and display it to the user
        truth = truths.pop()
        input(f"> {truth}")

        print("good answer")
        time.sleep(0.5)
        print(f"you have {lives} lives left")

    else:
        print("you lost a life!")
        time.sleep(1)
        print(f"you have {lives} lives left")
enzo
  • 9,861
  • 3
  • 15
  • 38
  • 1
    thank you! i have another question, there are not shown lists of questions for both truth and dare, how would I implement code to make it that if one of that list was randomly picked, it cannot be picked again? and how to print "you finished all dares" or "you finished all truths" after there is nothing left in the lists? – Jayden Jul 14 '21 at 14:32
  • @Jayden You can remove a random truth from the truths or a random dare from the dares using [this answer](https://stackoverflow.com/a/10048313/9997212). You can check if the truths or the dares are empty using [this answer](https://stackoverflow.com/a/53522/9997212). I think this is sufficient for you to work it out, but if you have any doubts you can post another question with a better explanation. – enzo Jul 14 '21 at 15:06
  • Next to your `input("> ")` statement. I'll update my answer. – enzo Jul 15 '21 at 17:12