在 Python 中制作一个非常简单的多项选择题时,如果两个选项都没有选择,我可以调用一行来重复吗?


  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 

我才刚刚开始学习python,这是我的第一门语言,所以我只是在玩它。else 语句是否可以再次请求输入,并在既不输入 A 也不输入 B 时将其保存为同一变量?

编辑:

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 :

像这样的东西,使用你的代码,while循环已被注释:

# 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.")

上面的示例只是如何完成该操作的示例。while 循环会一直要求键盘上的家伙输入"A"或"B"。然后你检查你的输入。

在实际代码中,您需要创建函数来捕获输入并进行所有花哨的检查。

在某些语言中,为了提高可读性,您有时会倾向于使用开关大小写结构而不是长 if-elif-else 语句。在 Python 中,没有 switch-case 语句,但您可以在字典中映射您的选择。函数也可以存储为变量。

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()

No.Python 不是一种命令式语言;尽管它确实支持临时编程。(即:Python中没有GOTO)。

您应该使用函数和循环。

例:

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

这将(非常微不足道地)继续打印"你想:A)接近房子。B)接近马厩。[A/B]?并在您进入Q时停止。我将由您来继续这种代码结构,并提供更多逻辑以适应您想要的实现。

注意:以这种风格编写会很快变得困难和复杂,最终你会想要将代码拆分为函数、模块等。

# 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")

相关内容

最新更新