代码工作,但不能正确的方式-python

时间:2016-08-15 00:21:37

标签: python

我正在尝试在python 3.4.3中创建这个修改版的公路旅行,它不能像我想要的那样工作。问题在于当玩家选择除了" y"当你因为卡住而受到打击时,它假设玩家想要在他/她没有机会时抓住机会。我试过使用else语句,我尝试了不在它前面。你会在我的代码中看到它。选择奖品或继续进行同样的事情。我知道它必须是同样的错误。这只是第一轮,所以我将添加更多,但我需要在我继续之前解决这个问题。提前谢谢。

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

2 个答案:

答案 0 :(得分:0)

您应该能够通过将第二个嵌套的if语句更改为elif来解决您的问题,如下所示:

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

查找if,elif,else语句的示例。 如果可能,您应该尝试尽可能多地删除嵌套的if语句。

示例:

if chance_taken1 == 2:
    return value
elif chance_taken1 == 1
    return other value
else:
    print "invalid input"
    return

答案 1 :(得分:0)

这样的测试不起作用:

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

总是如此。你需要这样做:

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

或者你可以这样做:

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

除此之外,你的代码只是凌乱和冗余(让你的空格键固定。)这里有一个返工,可能会给你一些关于如何编写游戏的想法,以便你可以更容易地扩展它(我已经删除了time.sleep()调用以专注于逻辑本身。)

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