判断字符串是否在字典键中,貌似写成了死循环
题:判断输入的罗马字符,是否都在字典x的键中,如在, 则输出对应的罗马字符#定义罗马字符及对应数字字典
x = {"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
#获取输入的字符
def lmzf():
a = input("请输入一个罗马字符:")
for i in a:
while i not in x.keys():
a = input("请输入一个罗马字符:")
print(a)
lmzf() 你输入的是字符还是字符串?为什么要用循环?
#定义罗马字符及对应数字字典
x = {"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
#获取输入的字符
def lmzf():
a = input("请输入一个罗马字符:")
return print(x) if a in x else None
lmzf() 傻眼貓咪 发表于 2022-7-3 21:23
你输入的是字符还是字符串?为什么要用循环?
字符串 Zhanghong528 发表于 2022-7-3 21:30
字符串
"""
题:
判断输入的罗马字符,是否都在字典x的键中,如在, 则输出对应的罗马字符
"""
# 定义罗马字符及对应数字字典
x = { "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000 }
# 获取输入的字符
def lmzf():
flag = True
while flag:
a = input("请输入一个罗马字符:")
flag = False
for i in a:
if i not in x:
flag = True
break
else:
print(x)
lmzf()
页:
[1]