关于if语句:在Python中创建一个非常简单的多项选择故事时,如果没有选择这两个选项,我可以调用一行来重复

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

本问题已经有最佳答案,请猛点这里访问。
1
2
3
4
5
6
  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时将其保存为相同的变量。

P.S.对这个问题的完全幼稚感到抱歉,事实证明是这样的。

编辑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 :

我对代码和故事都感到抱歉,但我会感谢您的帮助。如果你有什么更好的方法来学习语言的基础知识,以及构建这种类型的故事的建议,我很想知道:d


类似于这样,使用您的代码,而循环已被注释:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 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"。然后检查输入。

在真正的代码中,您将希望创建函数来捕获输入并进行所有花哨的检查。


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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()


不,python不是一种命令式语言;尽管它确实支持有意义的编程。(即:Python中没有goto)。

您应该使用函数和循环。

例子:

1
2
3
4
5
6
7
8
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时停止。我将由您决定用更多的逻辑来继续这种代码结构,以满足您所期望的实现。

注意:用这种方式写作会很快变得困难和复杂,最终你会想把你的代码分成函数、模块等。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# 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:
\
1.28
\
2.14
\
3.38
\
4.34
"

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:
\
1.12409
\
2.124081
\
3.14321
\
4.12408
"

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