英文:
What is wrong in my code if all my if statements are being ignored?
问题
I am a beginner and don't know what I've done wrong, help.
我是新手,不知道我做错了什么,请帮忙。
I tried multiple things, but all didn't work.
我尝试了多种方法,但都没有成功。
英文:
enter image description here
I am a begginer and don't know what I've done wrong, help.
I tried multiple things, but all didn't work.
答案1
得分: 1
所以这里有几个问题。你声明了一个整数类型的变量p,并赋值为`p=0`,但你在while循环中将其用作布尔值,同时你在`if else`语句中试图将其用作字符串输入。考虑使用更多的变量来达到你的目的。
首先,将`p=0`替换为`p=True`。让它成为你的循环标志。
其次,在循环内部,创建一个新的变量,比如q。
`q = int(input(这里是你的文本))`
第三,对于if else条件,由于我们有了一个整数类型的q,可以这样做:
```python
if q == 1:
print('你选择了1。')
elif q == 2:
print('你选择了2。') #以此类推
最终你的代码可能看起来像这样:
def 菜单():
p = True
while p:
q = int(input('输入值:'))
if q == 1:
print('你选择了1')
elif q == 2:
print('你选择了2')
elif q == 3:
print('你选择了3')
菜单()
<details>
<summary>英文:</summary>
So there are a couple of things going wrong here. You have declared a variable p as integer type with `p=0`, yet you use it like a bool in the while loop and you are trying to use it like a string input in the `if else` statements. Consider using more variables for your purpose.
First, replace `p=0` with `p=True`. Let this be your loop flag.
Second, inside the loop, make a new variable say q.
`q = int(input(`Your text here`))`
Third, for the if else condition, since we have an integer type q, do this:
if q == 1:
print('You selected 1.')
elif q == 2:
print('You selected 2.') #and so on
Ultimately your code might look something like this:
def menu():
p = True
while p:
q = int(input('Enter the value:'))
if q == 1:
print('You selected 1')
elif q == 2:
print('You selected 2')
elif q == 3:
print('You selected 3')
menu()
</details>
# 答案2
**得分**: 1
You can use "while True," which is equivalent to what you wrote. You can use a list with the values you need, and if your entered information is in that list, then we display the entered information, and if not, then again.
```python
def menu3():
while True:
q = int(input('输入文本1:'))
q_true = [1, 2, 3]
if q in q_true:
print(f'文本2:{q}')
menu3()
英文:
you can use while True, equivalent to what you wrote. You can use a list with the values that you need, and if your entered information is in that list, then we display the entered information, and if not, then again
def menu3():
while True:
q = int(input('*you text 1* '))
q_true = [1, 2, 3]
if q in q_true:
print(f'*you text 2* {q}')
menu3()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论