组织与医生的约会

2024-05-23 18:36:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个非常新的编码,我一直在工作的代码,组织与医生预约。你知道吗

问题是我的代码从每个列表中打印一个名字,而不是一个名字。代码是用python3.0在repl.it中编写的。你知道吗

category=input("Specialty number:")
import random
doctor1_list=["Dr. Fischer","Dr. Smith","Dr. Robertson"]
doctor1=doctor1_list[random.randint(0,len(doctor1_list)-1)]
print(doctor1)
if age < 18 and category == 1 :
    print()

import random
doctor2_list=["Dr. Rosenberg","Dr. Shriller","Dr. Coffman"]
doctor2=doctor2_list[random.randint(0,len(doctor2_list)-1)]
print(doctor2)
if age >= 18 and category == 1 :
    print()

import random
doctor3_list=["Dr. Gordon","Dr. Morgal","Dr. Richmin"]
doctor3=doctor3_list[random.randint(0,len(doctor3_list)-1)]
print(doctor3)
if age < 18 and category == 2 :
    print()

import random
doctor4_list=["Dr. Rodenthal","Dr. Wagabung","Dr. Stiller"]
doctor4=doctor4_list[random.randint(0,len(doctor4_list)-1)]
print(doctor4)
if age >= 18 and category == 2 :
    print()

import random
doctor5_list=["Dr. Rigardo","Dr. Ricardo","Dr. Rich"]
doctor5=doctor5_list[random.randint(0,len(doctor5_list)-1)]
print(doctor5)
if age < 18 and category == 3 :
    print()

Tags: andimportagelenifrandomlistprint
1条回答
网友
1楼 · 发布于 2024-05-23 18:36:21

你的问题是你没有在每一个条款的末尾都有一个条件检查。医生打印出来后,检查就完成了。我的建议是使用嵌套列表创建一个变量:

# Layer 1: each catergory (0, 1, or 2).
doctors = [
    [
        # Layer 2: 0 if under 18, 1 if over 18.
        ["Dr. Fischer"," Dr. Smith", "Dr. Robertson"],
        ["Dr. Rosenberg", "Dr. Shriller", "Dr. Coffman"]
    ], [
        ["Dr. Gordon","Dr. Morgal","Dr. Richmin"],
        ["Dr. Rodenthal","Dr. Wagabung","Dr. Stiller"],
    ], [
        ["Dr. Rigardo","Dr. Ricardo","Dr. Rich"]
        # Not given: catergory 3, over 18.
    ]
]

使用这些数据,您可以创建脚本:

import random

# Note I add int() encasing the input, to make category an integer.
category = int(input("Specialty number: "))

over18 = 1 if age >= 18 else 0

# Prints the doctor
print(random.choice(doctors[category][over18]))

相关问题 更多 >