-4

I am trying to create this modified version of road trip in python 3.4.3 and it is not working like I want. The problem is that when the player selects anything but a "y" when taking a hit for getting stuck, it assumes the player wants to take a chance when he/she doesn't. I have tried use the else statement and I tried the not in front of it. You will see that in my code. This same thing occurs for choosing a prize or to continue. I know it has to be the same error. This is just the first round so I will be adding more to it but I need to get this solved before I can continue. Thanks in advance.

import random
import time
import math
print("Welcome to ATV Road Trip!!!")
time.sleep(2)
stuck=0
print("Good luck because your gonna need it...")
time.sleep(2)
first_round=0
second_round=0
third_round=0
while True:
    print("You need 250 miles to complete the first round")
    time.sleep(2)
    first_number_1=random.choice([100,200,300])
    second_number_1=random.choice([0,10,20,30,40,50,60,70,80,90])
    third_number_1=random.choice([0,1,2,3,4,5,6,7,8,9])
    mystery_1=first_number_1+second_number_1+third_number_1
    first_roll_1=random.choice([25,25,25,35,35,35,50,50,50,75,75,75,100,100,150,150,200,mystery_1,mystery_1,mystery_1])
    if first_roll_1==mystery_1:
        print("Mystery miles!!!")
        time.sleep(2)
    print("You got a mile of:",first_roll_1)
    first_round=first_round+first_roll_1
    time.sleep(2)
    print("Your total miles so far in this round is:", first_round)
    time.sleep(3)
    first_number_2=random.choice([100,200,300])
    second_number_2=random.choice([0,10,20,30,40,50,60,70,80,90])
    third_number_2=random.choice([0,1,2,3,4,5,6,7,8,9])
    mystery_2=first_number_2+second_number_2+third_number_2
    first_roll_2=random.choice([25,25,25,35,35,35,50,50,50,75,75,75,100,100,150,150,200,mystery_2,mystery_2,mystery_2])
    print("Rolling again...")
    time.sleep(2)
    if first_roll_2==mystery_2:
        print("Mystery!!!")
        time.sleep(2)
    print("You got a mile of:", first_roll_2)
    first_round=first_round+first_roll_2
    time.sleep(2)
    print("Your total miles so far in this round is:", first_round)
    time.sleep(3)
    first_number_3=random.choice([100,200,300])
    second_number_3=random.choice([0,10,20,30,40,50,60,70,80,90])
    third_number_3=random.choice([0,1,2,3,4,5,6,7,8,9])
    mystery_3=first_number_3+second_number_3+third_number_3
    first_roll_3=random.choice([25,25,25,35,35,35,50,50,50,75,75,75,100,100,150,150,200,mystery_3,mystery_3,mystery_3])
    print("Rolling again...")
    time.sleep(2)
    if first_roll_3==mystery_3:
        print("Mystery!!!")
        time.sleep(2)
    print("You got a mile of:", first_roll_3)
    first_round=first_round+first_roll_3
    time.sleep(2)
    print("You got a total milage of:", first_round)
    time.sleep(3)
    print("Congratulations on your total milage, but you might get stuck!")
    time.sleep(4)
    print("Lets find out! You have a 50/50 shot of getting stuck!")
    time.sleep(3)
    print("""If you get stuck, you can choose to take the lose of 50 miles or
you can take a chance on another 50/50. If you win that, you get 50 miles added
to your total milage for this round and no points off. However, if you lose, then
you get 100 miles off! If you do not get stuck, nothing happens to your score.""")
    time.sleep(10)
    chance_1=random.randint(1,2)
    if chance_1==1:
        print("Congratulations on not getting stuck!!!")
        time.sleep(3)
    if chance_1==2:
        print("Sorry, you got stuck!!!")
        stuck=stuck+1
        print("It happens, sorry buddy.")
        time.sleep(4)
        lose_choice_1=input("Press y to take a chance of getting out and added score or n for taking the hit:")
        if lose_choice_1=='y' or 'Y':
            chance_taken_1=random.randint(1,2)
            if chance_taken_1==1:
                print("Congratulations on getting unstuck early!")
                print("You got 50 miles added!!!")
                first_round=first_round+50
                print("Your milage total is now:", first_round)
                time.sleep(5)
            if chance_taken_1==2:
                print("You got stuck again!")
                print("Ouch!!! 100 points off!!!")
                first_round=first_round-100
                print("Your milage total is now:", first_round)
                time.sleep(5)
        if not lose_choice_1=='y' or 'Y':
            print("You have decided to take the hit!")
            time.sleep(2)
            first_round=first_round-50
            print("Your mileage total is now:", first_round)
            time.sleep(3)
    if first_round<250:
        print("Sorry, you lost!")
        time.sleep(3)
        break
    if first_round>=250:
        print("Congratulations!!! You have passed the first round!!!")
        print("You can try for round 2 or you can select your prize of a rubber duck.")
        time.sleep(3)
        prize_pick_round_1=input("Press y to get prize and end game or any other key to continue:")
        if prize_pick_round_1=='y' or 'Y':
            print("You have chosen the rubber ducky!")
            print("I hope your happy with it!")
            print("Ending game...")
            time.sleep(5)
            break
        if not prize_pick_round_1=='y' or 'Y':
            continue
        break
Sean.D
  • 97
  • 8

2 Answers2

0

You should be able to fix your issue by changing the second nested if statement to elif like so:

import random
import time
import math
print("Welcome to ATV Road Trip!!!")
time.sleep(1)
stuck=0
print("Good luck because your gonna need it...")
time.sleep(1)
first_round=0
second_round=0
third_round=0
while True:
    print("You need 250 miles to complete the first round")
    time.sleep(1)
    first_number_1=random.choice([100,200,300])
    second_number_1=random.choice([0,10,20,30,40,50,60,70,80,90])
    third_number_1=random.choice([0,1,2,3,4,5,6,7,8,9])
    mystery_1=first_number_1+second_number_1+third_number_1
first_roll_1=random.choice([25,25,25,35,35,35,50,50,50,75,75,75,100,100,150,150,200,mystery_1,mystery_1,mystery_1])
    if first_roll_1==mystery_1:
        print("Mystery miles!!!")
        time.sleep(1)
    print("You got a mile of:",first_roll_1)
    first_round=first_round+first_roll_1
    time.sleep(1)
    print("Your total miles so far in this round is:", first_round)
    time.sleep(1)
    first_number_2=random.choice([100,200,300])
    second_number_2=random.choice([0,10,20,30,40,50,60,70,80,90])
    third_number_2=random.choice([0,1,2,3,4,5,6,7,8,9])
    mystery_2=first_number_2+second_number_2+third_number_2
first_roll_2=random.choice([25,25,25,35,35,35,50,50,50,75,75,75,100,100,150,150,200,mystery_2,mystery_2,mystery_2])
    print("Rolling again...")
    time.sleep(1)
    if first_roll_2==mystery_2:
        print("Mystery!!!")
        time.sleep(1)
    print("You got a mile of:", first_roll_2)
    first_round=first_round+first_roll_2
    time.sleep(1)
    print("Your total miles so far in this round is:", first_round)
    time.sleep(1)
    first_number_3=random.choice([100,200,300])
    second_number_3=random.choice([0,10,20,30,40,50,60,70,80,90])
    third_number_3=random.choice([0,1,2,3,4,5,6,7,8,9])
    mystery_3=first_number_3+second_number_3+third_number_3
first_roll_3=random.choice([25,25,25,35,35,35,50,50,50,75,75,75,100,100,150,150,200,mystery_3,mystery_3,mystery_3])
    print("Rolling again...")
    time.sleep(1)
    if first_roll_3==mystery_3:
        print("Mystery!!!")
        time.sleep(1)
    print("You got a mile of:", first_roll_3)
    first_round=first_round+first_roll_3
    time.sleep(1)
    print("You got a total milage of:", first_round)
    time.sleep(1)
    print("Congratulations on your total milage, but you might get stuck!")
    time.sleep(4)
    print("Lets find out! You have a 50/50 shot of getting stuck!")
    time.sleep(1)
    print("""If you get stuck, you can choose to take the lose of 50 miles or you can take a chance on another 50/50. If you win that, you get 50 miles added to your total milage for this round and no points off. However, if you lose, then you get 100 miles off! If you do not get stuck, nothing happens to your score.""")
    time.sleep(10)
    chance_1=random.randint(1,2)
    if chance_1 == 1:
        print("Congratulations on not getting stuck!!!")
        time.sleep(1)
    if chance_1 == 2:
        print("Sorry, you got stuck!!!")
        stuck=stuck+1
        print("It happens, sorry buddy.")
        time.sleep(4)
        lose_choice_1=raw_input("Press y to take a chance of getting out and added score or n for taking the hit:")
        if lose_choice_1 == 'y' or 'Y':
             chance_taken_1=random.randint(1,2)
             if chance_taken_1 == 1:
                print("Congratulations on getting unstuck early!")
                print("You got 50 miles added!!!")
                first_round=first_round+50
                print("Your milage total is now:", first_round)
                time.sleep(5)
            else:
                print("You got stuck again!")
                print("Ouch!!! 100 points off!!!")
                first_round=first_round-100
                print("Your milage total is now:", first_round)
                time.sleep(5)
        if lose_choice_1 != 'y' or 'Y':
             print("You have decided to take the hit!")
             time.sleep(1)
             first_round=first_round-50
             print("Your mileage total is now:", first_round)
             time.sleep(1)
    if first_round < 250:
        print("Sorry, you lost!")
        time.sleep(1)
        break
    if first_round >= 250:
        print("Congratulations!!! You have passed the first round!!!")
        print("You can try for round 2 or you can select your prize of a rubber duck.")
        time.sleep(1)
        prize_pick_round_1=raw_input("Press y to get prize and end game or any other key to continue:")
        if prize_pick_round_1 == 'y' or 'Y':
             print("You have chosen the rubber ducky!")
             print("I hope your happy with it!")
             print("Ending game...")
             time.sleep(5)
             break
        if prize_pick_round_1 != 'y' or 'Y':
             continue
        break

Look up examples of if, elif, else statements. When possible you should try to get rid of as many nested if statements as possible.

Example:

if chance_taken1 == 2:
    return value
elif chance_taken1 == 1
    return other value
else:
    print "invalid input"
    return
Lance
  • 86
  • 4
  • I am confused because I thought you could do else? It has to be elif then? So after my first if statement, then it has to be elif? – Sean.D Aug 15 '16 at 01:27
  • it doesn't have to be elif, you can use else, but if you do if else then you only have those two options. you can do if, elif, elif, elif, else and now you have five options you can iterate over in one if statement. – Lance Aug 15 '16 at 01:41
  • Ok, so I have the "if"and then if that "if" does not work, I have the elif. It still did not work. I only have 2 things in there so I would not need a 3 or 4th elif statement. Still confused here. Are you saying like over the Entire code I need to have if, elif, elif, then else like in a pattern? What if it is just one if statement and does not need an elif or else? – Sean.D Aug 15 '16 at 01:45
  • you can use if and else only if you need to. you do not need to use elif. i was merely trying to tell you that you could use it. – Lance Aug 15 '16 at 02:03
  • Oh ok. Well, it still is not working. Is there any thing else I can do? – Sean.D Aug 15 '16 at 02:09
  • Ok, I just realize I can look at the code you provided and you used an "!." What does this do? Is that like a not statement or something? – Sean.D Aug 15 '16 at 02:15
  • I just fixed almost all the bugs on my machine, used raw_input() instead of input. changed a couple of if not statements to != (not equals). didn't want to wait all the time so changed a bunch of the sleep timers to 1. – Lance Aug 15 '16 at 02:15
  • Unidented does not meet any outer undented level. This is an error I got around an else in the code you provided. Something like that. I am sure you can recognize it. What is wrong? – Sean.D Aug 15 '16 at 02:26
  • whitespace needs to be adjusted... https://gist.github.com/lanerjo/a258c595a02f3b2ae2decd4030fc0789 – Lance Aug 15 '16 at 02:38
  • Ok this time it said raw_input needs to be defined. So I left it at just input and it still is giving me the same problem as I started with. Running out of ideas here. – Sean.D Aug 15 '16 at 02:48
  • copy and paste from the gist i posted above that is your code just fixed – Lance Aug 15 '16 at 02:57
  • I did that but it said to define raw_input. Still giving me problems. Are you using python 3.4.3? – Sean.D Aug 15 '16 at 03:09
0

Tests like this don't work:

if lose_choice_1 == 'y' or 'Y':

It's always true. You need to do:

if lose_choice_1 == 'y' or lose_choice_1 == 'Y':

or you can do:

if lose_choice_1.lower() == 'y': 

Other than that, your code is just messy and redundant (get your space bar fixed.) Here's a rework that might give you some ideas about how to go about writing your game so that you can more easily extend it (I've removed the time.sleep() calls to focus on the logic itself.)

import random

print("Welcome to ATV Road Trip!!!")
print("Good luck because your gonna need it...")

stuck = 0
first_round = second_round = third_round = 0

while True:
    print("You need 250 miles to complete the first round")

    for roll in range(3):

        first_number = random.choice(range(100, 400, 100))
        second_number = random.choice(range(0, 100, 10))
        third_number = random.choice(range(10))

        mystery = first_number + second_number + third_number

        first_roll = random.choice([25, 35, 50, 75] * 3 + [100, 150] * 2 + [200, mystery, mystery, mystery])

        if roll > 0:
            print("Rolling again...")

        if first_roll == mystery:
            print("Mystery miles!!!")

        print("You added a mileage of:", first_roll)
        first_round += first_roll

        print("Your total miles so far in this round is:", first_round)


    print("Congratulations on your total mileage, but you might get stuck!")

    print("Lets find out! You have a 50/50 shot of getting stuck!")

    print("""If you get stuck, you can choose to take the loss of 50 miles or
you can take a chance on another 50/50. If you win that, you get 50 miles added
to your total mileage for this round and no points off. However, if you lose, then
you get 100 miles off! If you do not get stuck, nothing happens to your score.""")

    chance_1 = random.randint(1, 2)

    if chance_1 == 1:
        print("Congratulations on not getting stuck!!!")
    else:
        print("Sorry, you got stuck!!!")
        print("It happens, sorry buddy.")
        stuck += 1
        lose_choice_1 = input("Press y to take a chance of getting out and added score or n for taking the hit: ")

        if lose_choice_1 == 'y' or lose_choice_1 == 'Y':
            chance_taken_1 = random.randint(1, 2)

            if chance_taken_1 == 1:
                print("Congratulations on getting unstuck early!")
                print("You got 50 miles added!!!")
                first_round += 50
            else:
                print("You got stuck again!")
                print("Ouch!!! 100 points off!!!")
                first_round -= 100
        else:
            print("You have decided to take the hit!")
            first_round -= 50

        print("Your mileage total is now:", first_round)

    if first_round < 250:
        print("Sorry, you lost!")
        break
    else:
        print("Congratulations!!! You have passed the first round!!!")
        print("You can try for round 2 or you can select your prize of a rubber duck.")
        prize_pick_round_1 = input("Press y to get prize and end game or any other key to continue: ")

        if prize_pick_round_1 == 'y' or prize_pick_round_1 == 'Y':
            print("You have chosen the rubber ducky!")
            print("I hope your happy with it!")
            print("Ending game...")
            break
cdlane
  • 40,441
  • 5
  • 32
  • 81