2016-03-22 11 views
2

Python 3.4.3'te metin tabanlı bir macera oyunu oluşturuyorum ve kodun nasıl tekrarlanacağını anlayamıyorum. Daha önce neler olduğunu anlamaya yardımcı olursa, bundan önce bir demet anlatım var.Kodumda sorulanları tekrarlayamıyorum

print("\n\n\n\n\nYou awake to find yourself in the center of a clearing in a forest.") 
print("You stand up and decide to take a look around.") 

str = input("Which direction do you steer your head? d= down, l= left, r= right, u= up, b= behind you: ") 
print(" ") 
if str in ("d"): 
    print("You see your combat boots and the grassy ground below your feet. ") 

if str in ("l"): 
    print("The forest trees grow thicker and darker that way. You stare into the shadows and feel... cold...") 

if str in ("r"): 
    print("The forest is warm and inviting that way, you think you can hear a distant birds chirp.") 

if str in ("u"): 
    print("The blue sky looks gorgeous, a crow flies overhead... that's not a crow...") 
    print("It's a Nevermore, an aerial Grim. You stand still until it passes.") 

if str in ("b"): 
    print("the grass slowly grows to dirt as the area falls into a mountain cliff. You now know where you are.") 
    print("Mount Glenn, one of the most Grim-infested places in all of Remnant.") 
    print("It's a bit unsettling.") 

else: 
    print("Try that again") 

Onlar her mümkün cevabı yanıtladı ve sonraki soruya geçmek yapana kadar kod, kullanıcıya soruyu tekrarlamak istiyorum. Ayrıca, başka aldıklarında da soruyu tekrar etmelerini istiyorum. Bunu nasıl yaparım? Böyle

+1

Bir ara döngü kullanın, bir kesme koşulu sağlandığında ayrılın. – DhruvPathak

+0

Döngüyü anlamıyorum. Onlar ... sinir bozucu. Bunun için nasıl giderim? –

+0

Neden "str" ​​("d") 'yi kullanıyorsunuz ve str == d' değil? – ASCIIThenANSI

cevap

0

Do şey:

answered = False 
while not answered: 
    str = input("Question") 
    if str == "Desired answer": 
     answered = True 
1

Temel olarak bir döngü içinde sorunuzu koymak ve vaka 'eğer' arzu birini girin kadar bu yineleyebilirsiniz. Kodunuzu aşağıdaki gibi değiştirdim. Önemli bir yerleşik gölge ve tuhaf sorunlara neden olur, bir göz değişken adı olarak

print("\n\n\n\n\nYou awake to find yourself in the center of a clearing in a forest.") 
print("You stand up and decide to take a look around.") 

while True: 
    str = input("Which direction do you steer your head? d= down, l= left, r= right, u= up, b= behind you: ") 
    print(" ") 
    if str in ("d"): 
     print("You see your combat boots and the grassy ground below your feet. ") 
     break 

    if str in ("l"): 
     print("The forest trees grow thicker and darker that way. You stare into the shadows and feel... cold...") 
     break 

    if str in ("r"): 
     print("The forest is warm and inviting that way, you think you can hear a distant birds chirp.") 
     break 

    if str in ("u"): 
     print("The blue sky looks gorgeous, a crow flies overhead... that's not a crow...") 
     print("It's a Nevermore, an aerial Grim. You stand still until it passes.") 
     break 

    if str in ("b"): 
     print("the grass slowly grows to dirt as the area falls into a mountain cliff. You now know where you are.") 
     print("Mount Glenn, one of the most Grim-infested places in all of Remnant.") 
     print("It's a bit unsettling.") 
     break 

    else: 
     print("Try that again") 
+0

Neato! Teşekkür ederim! Yani "kırılma", bu, zaman döngüsünün dışına sıçramayı ve bir sonraki bölüme geçmeyi sağlıyor mu? Yani kırılmıyor, devam ediyor, anlayışımı düzeltiyor muyum? –

+0

Evet, tam olarak. Eğer 'if' durumundan birine girerse, break komutu while döngüsünden çıkar. Aksi takdirde, aynı döngüyü tekrar tekrar yürütmeye devam eder – newbie

+0

İnanılmazsınız, teşekkür ederim. –

2

kullanmayın str bulundurun.

Çıkışı geçerli seçeneklerle sınırlamak için bir süre döngü kullanın.

valid_choices = ('d', 'l', 'r', 'u', 'b',) 

choice = None 
while choice not in valid_choices: 
    text = input("Which direction do you steer your head? d= down, l= left, r= right, u= up, b= behind you: ") 
    choice = text.strip() 

if choice == 'd': 
    print ('...') 
elif choice == 'u': 
    print ('...') 

Ayrıca bakınız:

+0

Teşekkür ederiz! Bu çok yardımcı oldu! Sormak istiyorum, text.strip ne yapar, ne yapar() yapar ve seçim nedir = yok? –

+0

'text.strip()', boşlukları kaldıracaktır. Parens sözdizimi bir “tuple”, bir “list” e benziyor ama değişmez. 'Yok', özel bir geçersiz değeri temsil eden bir sabittir. https://docs.python.org/2/library/constants.html#None –

+0

İlginç! Yani, değişmez olduğundan, normalde ne ile çatışabileceğinden bağımsız olarak, doğru olarak gösterilecek? Ve hiçbiri için teşekkür ederim. –

0

İşte bu yapacağını nasıl; açıklama yorumların içinde:

# Print out the start text 
print("\n\n\n\n\nYou awake to find yourself in the center of a clearing in a forest.") 
print("You stand up and decide to take a look around.") 

# Use a function to get the direction; saves some repeating later 
def get_direction(): 
     answer = input("Which direction do you steer your head? d= down, l= left, r= right, u= up, b= behind you: ") 
     print(" ") 
     return answer 

# Keep running this block until the condition is False. 
# In this case, the condition is True, so it keeps running forever 
# Until we tell Python to "break" the loop. 
while True: 
     # I changed "str" to "answer" here because "str" is already a Python 
     # built-in. It will work for now, but you'll get confused later on. 
     answer = get_direction() 

     if answer == "d": 
       print("You see your combat boots and the grassy ground below your feet. ") 

       # Stop the loop 
       break 
     elif answer == "l": 
       print("The forest trees grow thicker and darker that way. You stare into the shadows and feel... cold...") 
       break 
     elif answer == "r": 
       print("The forest is warm and inviting that way, you think you can hear a distant birds chirp.") 
       break 
     elif answer == "u": 
       print("The blue sky looks gorgeous, a crow flies overhead... that's not a crow...") 
       print("It's a Nevermore, an aerial Grim. You stand still until it passes.") 
       break 
     elif answer == "b": 
       print("the grass slowly grows to dirt as the area falls into a mountain cliff. You now know where you are.") 
       print("Mount Glenn, one of the most Grim-infested places in all of Remnant.") 
       print("It's a bit unsettling.") 
       break 
     else: 
       print("Try that again") 

       # NO break here! This means we start over again from the top 

Şimdi, bunların hiçbiri birkaç yönden daha eklerseniz çok iyi ölçekler; Gitmene sonra bu döngü içinde yeni döngü yani "doğru" vb, yeni bir soru istediğinizi varsayalım çünkü

# The start text 
print("\n\n\n\n\nYou awake to find yourself in the center of a clearing in a forest.") 
print("You stand up and decide to take a look around.") 

# Use a function to get the direction 
def get_direction(): 
    answer = input("Which direction do you steer your head? d= down, l= left, r= right, u= up, b= behind you: ") 
    print(" ") 
    return answer 


# Use a function to store a "location" and the various descriptions that 
# apply to it 
def location_start(): 
    return { 
     'down': [ 
      # Function name of the location we go to 
      'location_foo', 

      # Description of this 
      'You see your combat boots and the grassy ground below your feet.' 
     ], 

     'left': [ 
      'location_bar', 
      'The forest trees grow thicker and darker that way. You stare into the shadows and feel... cold...' 
     ], 

     'right': [ 
      'location_other', 
      'The forest is warm and inviting that way, you think you can hear a distant birds chirp.' 
     ], 

     'up': [ 
      'location_more', 
      "The blue sky looks gorgeous, a crow flies overhead... that's not a crow...\n" + 
       "It's a Nevermore, an aerial Grim. You stand still until it passes." 
     ], 

     'behind': [ 
      'location_and_so_forth', 
      "The grass slowly grows to dirt as the area falls into a mountain cliff. You now know where you are.\n" + 
       "Mount Glenn, one of the most Grim-infested places in all of Remnant.\n" + 
       "It's a bit unsettling." 
     ], 
    } 

# And another location ... You'll probably add a bunch more... 
def location_foo(): 
    return { 
     'down': [ 
      'location_such_and_such', 
      'desc...' 
     ], 
    } 

# Store the current location 
current_location = location_start 

# Keep running this block until the condition is False. 
# In this case, the condition is True, so it keeps running forever 
# Until we tell Python to "break" the loop. 
while True: 
    # Run the function for our current location 
    loc = current_location() 
    answer = get_direction() 

    if answer == ("d"): 
     direction = 'down' 
    elif answer == ("l"): 
     direction = 'left' 
    elif answer == ("r"): 
     direction = 'right' 
    elif answer == ("u"): 
     direction = 'up' 
    elif answer == ("b"): 
     direction = 'behind' 
    else: 
     print("Try that again") 

     # Continue to the next iteration of the loop. Prevents the code below 
     # from being run 
     continue 

    # print out the key from the dict 
    print(loc[direction][1]) 

    # Set the new current location. When this loop starts from the top, 
    # loc = current_location() is now something different! 
    current_location = globals()[loc[direction][0]] 

Şimdi, bu yapıyor sadece biri yoludur; Burada bir dezavantaj oyuncu farklı yönlerden bir yere yaklaşma izin vermek istiyorsanız konumlar için açıklamaları tekrarlamak için gerekir. Bu, macera oyununuza uygulanamaz (, 'u doğru hatırlıyorumsa, buna izin vermez).
Bunu oldukça kolay bir şekilde düzeltebilirsiniz, ancak bunu size bir egzersiz olarak bırakacağım ;-)

İlgili konular