2013-12-10 3 views
-5

У меня есть школьный проект, над которым я работал, но я не могу работать. Я пробовал перемещаться по while True, но я либо получаю бесконечный цикл, либо доходит до последнего ввода и только возвращается к началу.Бесконечная петля и не получая ожидаемых результатов

import random 
while True: 
    dice1=random.randint (0,7) 
    dice2=random.randint (0,7) 
    break 

strengthone = int(input ("Player 1, between 1 and 10 What do you want your characters strength to be? Higher is not always better.")) 
skillone = int(input ("Player 1, between 1 and 10 What do you want your characters skill to be? Higher is not always better.")) 
while True: 
    if strengthone > 10: 
     print ("Incorrect value") 
    else: 
     print ("Good choice.") 
    if skillone > 10: 
     print ("Incorrect value.") 
    else: 
     print ("Good choice.") 

    strengthtwo = int(input ("Player 2, between 1 and 10 what do you want your characters strength to be? Higher is not always better.")) 
    skilltwo = int(input ("Player 2, between 1 and 10 what do you want your characters skill to be? Higher is not always better.")) 

    if strengthtwo > 10: 
     print ("Incorrect value.") 
    else: 
     print ("Good choice.") 
    if skillone > 10: 
     print ("Incorrect value.") 
    else: 
     print ("Good choice.") 
while True: 
    strengthmod = strengthone - strengthtwo 
    skillmod = skillone - skilltwo 
    strengthmodone = strengthone - strengthtwo 
    skillmodone = skillone - skilltwo 
    strengthmodtwo = strengthone - strengthtwo 
    skillmodtwo = skillone - skilltwo 

    print ("Player 1, you rolled a", str(dice1)) 
    print ("Player 2, you rolled a", str(dice2)) 


    if dice1 == dice2: 
     print ("") 
    elif dice1 > dice2: 
     strengthmodone = strengthmod + strengthone 
     strengthmodone = strengthmod + strengthone 
    elif dice2 > dice1: 
     strengthmodtwo = strengthmod + strengthtwo 
     skillmodtwo = skillmod + skilltwo 
    elif dice1 < dice2: 
     strengthmodone = strengthmod - strengthone 
     skillmodone= skillmod - skillone 
    else: 
     dice2 < dice1 
     strengthmodtwo = strengthmod - strengthtwo 
     skillmodtwo = skillmod - skilltwo 
    while True: 
     strengthmodone = strengthmodone - dice1 
     strengthmodtwo = strengthmodtwo - dice2 
     break 
     while True: 
      if strengthmodone == 0: 
       print ("Player one dies, well done player two. You win!") 
      elif strengthmodtwo == 0: 
       print ("Player two dies, well done player one. You win!") 
      elif strengthmodone > 0: 
       strengthmodone - 1 
      else: 
       strengthmodtwo == 0 
       strengthmodtwo - 1 
       break 
       break 

Любая помощь очень ценится :)

+0

Я бы не рекомендовал использовать 'while True', если вам не нужно - знаете ли вы, что он делает? –

+0

Определите проблему конкретно. Что случилось? Где вы думаете, что все идет не так? Что ожидается? etc –

+0

Я не думаю, что вы понимаете, для чего используется цикл while. Возможно, вам стоит вернуться к основам и узнать, как они работают и когда их следует использовать. – Tim

ответ

1

Некоторые вещи, которые, безусловно, не работают:

elif dice2 > dice1: 
    strengthmodtwo = strengthmod + strengthtwo 
    skillmodtwo = skillmod + skilltwo 
elif dice1 < dice2: # you will never reach this code, because it is the same condition as the one above 
    strengthmodone = strengthmod - strengthone 
    skillmodone= skillmod - skillone 

Эти два условия выше такие же, как раз наоборот.

while True: 
    strengthmodone = strengthmodone - dice1 
    strengthmodtwo = strengthmodtwo - dice2 
    break 
    while True: # you will never reach this while loop, because of the break that stops the outer loop 

Вам не нужен цикл для кода, который вы хотите запустить только один раз. Что вы делаете с

while True: 
    do_something 
    break 

выполняет do_something ровно один раз.

strengthmodtwo == 0 # it checks for equality, what you want is an assignment (=) 

strengthmodtwo - 1 # you have to assign the newly computed value, either: 
strengthmodtwo -= 1 # or 
strengthmodtwo = strengthmodtwo - 1 

Вы должны заботиться о разных операторах. Операторы сравнения: ==, < =, < = ... операторы присваивания: =, - =, + = вычислительных операторов: -, *, ... (ничего не происходит с самого значения)

Как использовать петлю на примере печати 1 до 10:

# example 1 
i = 1 
while True: 
    print i 
    i += 1 
    if i > 10: 
     break 


# example 2 
i = 1 
while i < 10: 
    print i 
    i += 1 

пример-намного лучше!

Надеюсь, я мог бы немного помочь, я бы рекомендовал прочитать некоторые уроки, есть тысячи доступных.

+1

У Python нет оператора '++' increment; используйте 'i + = 1' для увеличения. Использование точек с запятой разрешено, но сильно не рекомендуется. – Tim

Смежные вопросы