When making a very simple multiple choice story in Python, can i call a line to repeat if neither of the options are selected [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 16:49:27

问题


  d1a = input ("Do you want to: A) Approach the house. B) Approach the stable. [A/B]? : ")
        if d1a == "A":
            print ("You approach the cottage.")
        elif d1a == "B":
            print ("You approach the stables.")
        else 

I have only just started learning python and it is my first language so I am just playing about with it, Is it possible for the else statement to ask for the input again and it to be saved as the same variable when neither A or B is typed in.

P.S sorry before hand for the complete naivety of this question it turns out to be so

EDIT:

import time
import random
import sys
print ("You wake up to find yourself in a clearing off a forest, sounded by tall")
print (" trees on all sides with a path ahead of you")
d1 = input ("Do you want to : A) Walk down the path. B)Move your way through the trees? [A/B]: ")
if d1 == "A":
    print ("You begin to walk down the path.")
    print (" As a Sidenote, during this adventure 'dice' will be thrown and the success of your chosen action")
    print (" will be determined by the result.")
    r1 = random.randint(0.0,10.0)
    if 4 > r1 > 0.1:
        print (" Your groggyness from waking means you reach the edge of the forest after nightfall.")
        print (" You see that the path continues towards a small cottage, the lights are out and no smoke rises from the chimney.")
        print (" Away from the cottage is a stable, where you can see a horse standing with its head outside the door")
        d1a = input ("Do you want to: A) Approach the house. B) Approach the stable. [A/B]? : ")
        if d1a == "A":
            print ("You approach the cottage.")
        elif d1a == "B":
             print ("You approach the stables.")
        else :

I am both sorry for the code and the story but I would appreciate the help. If you have any suggestions of a better way to learn the basics of the language as well as construct this type of story I would love to know :D


回答1:


Something like this, using your code, while loop has been annotated:

# gather the input
# "while" is the loop statement, checking the condition and executing the code in the body of loop while the condition holds true
# obviously, "while True" will execute its body forever until "break" statement executes or you press Ctrl+C on keyboard
while True:
    d1a = input ("Do you want to: A) Approach the house. B) Approach the stable. [A/B]? : ")
    # check if d1a is equal to one of the strings, specified in the list
    if d1a in ['A', 'B']:
        # if it was equal - break from the while loop
        break
# process the input
if d1a == "A": 
    print ("You approach the cottage.") 
elif d1a == "B": 
    print ("You approach the stables.")

The sample above is just an example of how to accomplish the thing. The while loop will keep asking the guy at the keyboard to enter 'A' or 'B'. Then you check your input.

In the real code you'll want to create function to capture the input and make all fancy checks.




回答2:


In some languages You would sometimes tend to use switch-case structures instead of long if-elif-else statements in order to improve readability. In Python, there is no switch-case statement, but You can map Your choices in a dictionary. Functions can be stored as variables too.

def opt_a():
  print("You approach the cottage.")

def opt_b():
  print("You approach the stables.")

def invalid_opt():
  print("Invalid choice")

options = {"A":["Approach the house",opt_a], "B":["Approach the stable",opt_b]}

for option in options:
  print(option+") "+options.get(option)[0])

choise = input("Please make Your choise: ")

val = options.get(choise)
if val is not None:
  action = val[1]
else:
  action = invalid_opt

action()



回答3:


No. Python is not an imperative language; although it does support impreative programming. (i.e: There is no GOTO in Python).

You should use either functions and loops.

Example:

while True:
    d1a = input ("Do you want to: A) Approach the house. B) Approach the stable. [A/B]? : ")
    if d1a == "A":
        print ("You approach the cottage.")
    elif d1a == "B":
        print ("You approach the stables.")
    elif dia == "Q":
        break

This will (very trivially) keep printing "Do you want to: A) Approach the house. B) Approach the stable. [A/B]?" and stop when you enter Q. I twould be up to you to continue this structure of code with more logic to suit your desired implementation.

Note: Writing in this style will get difficulty and complex very quickly and eventually you will want to split up your code into functions, modules, etc.




回答4:


# this code below welcomes the user 
print ("hello and welcome to my python quiz,my name is brandon and i am the quiz master")

print ("please type your name")

your_name = input ()

your_name = str(your_name)

score = 0# this code set the veriable to zero
# this is the question
print ("Question 1")

print ("what is 50x10")
answer = input ()
answer =int(answer)

if answer == 500:
   print ("good work")
   score = score + 1

else:

    print ("better luck next time")

    score = score - 1


print ("Question 2")

print ("what is (5x10)-4x2")
answer = input ()
answer =int(answer)

if answer == 94:
   print ("good work")
   score = score + 1

else:

    print ("better luck next time")

    score = score - 1

print ("Question 3")

print ("what is 600000000x10")
answer = input ()
answer =int(answer)

if answer == 6000000000:
   print ("good work")
   score = score + 1

else:

    print ("better luck next time")

    score = score - 1

print ("Question 4")

print ("what is 600000000/10")
answer = input ()
answer =int(answer)

if answer == 60000000:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1

print ("Question 5")

print ("what is 19x2-2")
answer = input ()
answer =int(answer)

if answer == 36:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1

print ("Question 6")

print ("what is (8x4) x 9")
answer = input ()
answer =int(answer)

if answer == 288:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1

print ("Question 7")

print ("what is 15 x4")
answer = input ()
answer =int(answer)

if answer == 60:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1

print ("Question 8")

print ("what is 19 x9")
answer = input ()
answer =int(answer)

if answer == 171:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1

print ("Question 9")

print ("what is 9 x9")
answer = input ()
answer =int(answer)

if answer == 81:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1

print ("Question 10")

print ("what is 10 x10")
answer = input ()
answer =int(answer)

if answer == 171:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1    


print ("Question 10")

print ("what is 10 x10")
answer = input ()
answer =int(answer)

if answer == 171:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1

print("Question 11")

print ("6+8x2: \n\
1.28 \n\
2.14 \n\
3.38 \n\
4.34 \n"
answer =int (input (Menu))

if answer ==   1.:
       print ("well done")
elif answer == 2.:
       print ("better luck next time")
elif answer == 3.:
       print ("looser")
elif answer == 4.:
       print ("go back to primary school and learn how to add")

print("Question 12")

print ("6194+10x2: \n\
1.12409 \n\
2.124081 \n\
3.14321 \n\
4.12408 \n"
       answer =int(input (Menu))

if answer ==   1.:
       print ("well done")
elif answer == 2.:
       print ("better luck next time")
elif answer == 3.:
       print ("looser")
elif answer == 4.:
       print ("go back to primary school and learn how to add")





if score > 8:# this line tells the program if the user scored 2 points or over to print the line below

   print("amazing your score is" + str(score))# this code tells the user that they have done well if they have got 2or more points or over

elif score < 4:
    print ("good work your score is" + str(score))
else:
    print ("better look next time your scor is" + str(score))

print("well done your score is " +str (score))#this print the users score

print("thank you for playing in the python maths quiz")


来源:https://stackoverflow.com/questions/21082037/when-making-a-very-simple-multiple-choice-story-in-python-can-i-call-a-line-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!