2016-08-15 2 views
-4

python 3.4.3에서이 수정 된로드 트립 버전을 만들고 싶습니다. 원하는대로 작동하지 않습니다. 문제는 플레이어가 멈추지 않을 때 히트를 칠 때 "y"이외의 것을 선택하면 플레이어가 그렇지 않을 때 기회를 원한다고 가정하는 것입니다. 나는 else 문을 사용하려고 시도했고 그것의 앞에 not를 시도했다. 당신은 내 코드에서 그것을 볼 수 있습니다. 이 같은 현상은 상을 선택하거나 계속 진행할 때 발생합니다. 나는 그것이 같은 오류가 있어야한다는 것을 안다. 이것은 첫 번째 라운드이므로 더 추가 할 예정이지만 계속 진행하기 전에 해결해야합니다. 미리 감사드립니다.코드가 작동하지만 올바른 방식으로 작동하지 않습니다 -python

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 
+0

[어떻게 할 수 있습니까? 여러 값에 대해 하나의 변수를 테스트합니까?] (http : //stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) – Makoto

+0

정확히 내 생각에는 생각하지 않습니다. –

+0

무엇이 될 수 있는지에 대한 아이디어가 있습니까? –

답변

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 
+0

이 덕분에 많은 도움이되었습니다. 나는 지금 내가 뭘 잘못했는지 안다. –

0

당신은 변경하여 문제를 해결할 수 있어야 문과 같이 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 

이 경우 ELIF, 다른 문장의 예를 검색합니다. 가능하면 많은 중첩 된 if 문을 제거해야합니다.

예 :이 같은

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

다른 일을 할 수 있다고 생각해서 혼란 스럽습니까? 그때 엘프 야? 그래서 첫 번째 if 문 다음에 엘프가 있어야하나요? –

+0

elif 일 필요는 없지만 else를 사용할 수는 있지만 그렇게하면 다른 두 옵션 만 사용할 수 있습니다. if, elif, elif, elif, else 그리고 if 문 하나에서 다섯 가지 옵션을 반복 할 수 있습니다. – Lance

+0

좋아, 그래서 "if"가 있고 "if"가 작동하지 않으면 elif가있다. 여전히 효과가 없었습니다. 나는 거기에 2 가지만 가지고 있으므로 3 또는 4 엘프 진술을 필요로하지 않을 것입니다. 아직도 혼란스러워. 당신이 전체 코드에 대해서 말하고있는 건지, 내가 엘프, 엘프, 그 다음에는 패턴을 좋아할 필요가 있니? 그것이 단지 하나의 진술이고 elif 또는 다른 것을 필요로하지 않는다면 어떨까요? –

관련 문제