gpt4 book ai didi

python - python中的简单C代码---switch语句

转载 作者:行者123 更新时间:2023-11-28 06:44:43 24 4
gpt4 key购买 nike

我想用 Python 编写这段 C++ 代码:

cout<<"input number";
cin>>x;
switch(x)
{
case '1':
cout<<"my name is ali";
case '2':
cout<<"my name is hamza";
default:
cout<<"invalid input";
}
goto again:

我也检查了字典语句,但可能我编码不正确。

最佳答案

这是一种方法,python 中没有 switch 语句:

options = {"1":"my name is ali","2":"my name is hamza"} # map responses to keys

while True:
x = raw_input("input number") # take user input, use `input()` for python 3
if x in options: # if the key exists in the options dict
print(options[x]) # print the appropriate response
break # end the loop
else:
print("invalid input") # or else input is invalid, ask again

关于python - python中的简单C代码---switch语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25217491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com