你的代码如果所有的if语句都被忽略,可能出现了问题。

huangapple go评论81阅读模式
英文:

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(&#39;*you text 1* &#39;))
        q_true = [1, 2, 3]
        if q in q_true:
            print(f&#39;*you text 2* {q}&#39;)

menu3()

huangapple
  • 本文由 发表于 2023年5月7日 21:46:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定