Hướng dẫn how do you create multiple choice inputs in python? - làm thế nào để bạn tạo đầu vào nhiều lựa chọn trong python?

Sử dụng trong khi đúng với câu lệnh IF, bạn có thể đưa ra một câu hỏi trắc nghiệm trong Python. Dừng vòng lặp nếu người dùng nhập vào Q Q.“Q”.

Mã ví dụ đơn giản Tạo một câu chuyện trắc nghiệm rất đơn giản trong Python, hãy lặp lại nếu cả hai tùy chọn đều không được chọn.

while True:
    d1a = input("Do you want to: \n A) House. B) Stable. [A/B]? : ")
    if d1a == "A":
        print("You approach the cottage.")
    elif d1a == "B":
        print("You approach the stables.")
    elif d1a == "Q":
        print("Done!")
        break

Output::

Hướng dẫn how do you create multiple choice inputs in python? - làm thế nào để bạn tạo đầu vào nhiều lựa chọn trong python?

Hãy bình luận nếu bạn có bất kỳ nghi ngờ hoặc đề xuất nào về mã chương trình Python này.

Lưu ý: IDE: & NBSP; Pycharm & NBSP; 2021.3.3 (Phiên bản cộng đồng) IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

Tất cả & nbsp; ví dụ python & nbsp; là trong & nbsp; Python & nbsp; 3, vì vậy có thể khác với các phiên bản Python 2 hoặc nâng cấp. Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Hướng dẫn how do you create multiple choice inputs in python? - làm thế nào để bạn tạo đầu vào nhiều lựa chọn trong python?

Bằng cấp về Khoa học máy tính và Kỹ sư: Nhà phát triển ứng dụng và có nhiều ngôn ngữ lập trình kinh nghiệm. Sự nhiệt tình cho công nghệ và thích học kỹ thuật.

  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 

Tôi chỉ mới bắt đầu học Python và đó là ngôn ngữ đầu tiên của tôi, vì vậy tôi chỉ đang chơi với nó. Có thể cho câu lệnh khác yêu cầu đầu vào một lần nữa và nó được lưu dưới dạng cùng một biến khi không A hoặc B được nhập vào?

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 :

Hướng dẫn how do you create multiple choice inputs in python? - làm thế nào để bạn tạo đầu vào nhiều lựa chọn trong python?

Peterh

11.1K15 Huy hiệu vàng80 Huy hiệu bạc102 Huy hiệu đồng15 gold badges80 silver badges102 bronze badges

Đã hỏi ngày 13 tháng 1 năm 2014 lúc 0:14Jan 13, 2014 at 0:14

Hướng dẫn how do you create multiple choice inputs in python? - làm thế nào để bạn tạo đầu vào nhiều lựa chọn trong python?

5

Một cái gì đó như thế này, sử dụng mã của bạn, trong khi vòng lặp đã được chú thích:

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

Mẫu trên chỉ là một ví dụ về cách hoàn thành điều đó. Vòng lặp trong khi sẽ tiếp tục yêu cầu anh chàng ở bàn phím nhập 'A' hoặc 'B'. Sau đó, bạn kiểm tra đầu vào của bạn.

Trong mã thực, bạn sẽ muốn tạo chức năng để nắm bắt đầu vào và thực hiện tất cả các kiểm tra ưa thích.

Đã trả lời ngày 13 tháng 1 năm 2014 lúc 0:22Jan 13, 2014 at 0:22

Andy Wandy wAndy W

1.9821 Huy hiệu vàng12 Huy hiệu bạc9 Huy hiệu đồng1 gold badge12 silver badges9 bronze badges

2

Trong một số ngôn ngữ, đôi khi bạn sẽ có xu hướng sử dụng các cấu trúc trường hợp chuyển đổi thay vì các câu lệnh IF-ELIF dài để cải thiện khả năng đọc. Trong Python, không có câu lệnh trong trường hợp chuyển đổi, nhưng bạn có thể ánh xạ các lựa chọn của mình trong một từ điển. Các chức năng có thể được lưu trữ dưới dạng các biến quá.

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

Đã trả lời ngày 13 tháng 1 năm 2014 lúc 3:18Jan 13, 2014 at 3:18

Không. Python không phải là một ngôn ngữ bắt buộc; Mặc dù nó hỗ trợ lập trình không thể thực hiện được. (tức là: không có goto trong Python).imperative language; although it does support impreative programming. (i.e: There is no GOTO in Python).

Bạn nên sử dụng một trong hai chức năng và vòng lặp.

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

Điều này sẽ (rất tầm thường) tiếp tục in "Bạn có muốn: a) tiếp cận ngôi nhà. B) Tiếp cận ổn định. [A/B]?" và dừng lại khi bạn vào Q. Tôi sẽ tùy thuộc vào bạn để tiếp tục cấu trúc mã này với nhiều logic hơn để phù hợp với việc thực hiện mong muốn của bạn.

Lưu ý: Viết theo kiểu này sẽ gặp khó khăn và phức tạp rất nhanh và cuối cùng bạn sẽ muốn chia mã của mình thành các chức năng, mô -đun, v.v. 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.

Đã trả lời ngày 13 tháng 1 năm 2014 lúc 0:16Jan 13, 2014 at 0:16

James Millsjames MillsJames Mills

18.1k3 Huy hiệu vàng46 Huy hiệu bạc59 Huy hiệu Đồng3 gold badges46 silver badges59 bronze badges

1

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

Filmor

29,3K4 Huy hiệu vàng49 Huy hiệu bạc48 Huy hiệu đồng4 gold badges49 silver badges48 bronze badges

Đã trả lời ngày 27 tháng 11 năm 2014 lúc 17:02Nov 27, 2014 at 17:02

Làm thế nào để bạn đưa ra lựa chọn trong Python?

Những điểm chính..
Sử dụng nếu điều kiện để bắt đầu một câu lệnh có điều kiện, điều kiện Elif để cung cấp các thử nghiệm bổ sung và nếu không để cung cấp mặc định ..
Các cơ thể của các nhánh của các tuyên bố có điều kiện phải được thụt vào ..
Sử dụng == để kiểm tra cho sự bình đẳng ..
X và y chỉ đúng nếu cả x và y đều đúng ..

Làm thế nào để bạn sử dụng các tùy chọn lựa chọn trong Python?

Lựa chọn () là một hàm sẵn có trong ngôn ngữ lập trình Python trả về một mục ngẫu nhiên từ danh sách, tuple hoặc chuỗi ...
Cú pháp: ngẫu nhiên.....
Lưu ý: Chúng tôi phải nhập ngẫu nhiên để sử dụng phương thức Lựa chọn ().....
Đầu ra mọi lúc sẽ khác nhau khi hệ thống trả về một mục ngẫu nhiên.....
Ứng dụng thực tế:.

Làm thế nào để bạn đưa ra một câu hỏi trắc nghiệm?

Thiết kế các lựa chọn thay thế..
Giới hạn số lượng các lựa chọn thay thế.....
Hãy chắc chắn rằng chỉ có một câu trả lời tốt nhất.....
Làm cho những kẻ phân tâm hấp dẫn và hợp lý.....
Làm cho các lựa chọn phù hợp về mặt tổng hợp với thân cây.....
Đặt các lựa chọn theo một số thứ tự có ý nghĩa.....
Phân phối ngẫu nhiên các phản ứng chính xác.....
Tránh sử dụng tất cả những người trên mạng ..

Làm thế nào để bạn mã hóa một câu hỏi trong Python?

Làm thế nào để thực hiện một câu hỏi có hoặc không có trong câu trả lời của mã Python..
Câu hỏi = đầu vào ("câu hỏi của bạn").
Nếu câu hỏi == ("Có").
In ("Làm tốt").
Elif câu hỏi == ("Không").
in ("thử lại").