Menlo 发表于 2023-10-11 18:24:07

Python,萌新求助,超简单问题,给鱼币

本帖最后由 Menlo 于 2023-10-11 18:45 编辑


counts=5

while counts>0:
    gender=input("what is your gender?")

    if gender.lower()=="boy":
      print("you are a male.")

    if gender.lower()=="girl":
      print("you are a female.")
      counts=counts-1
      
    else:
      print("what do you mean?")
      break



不知道到底哪里错了。。
想呈现的状态是
回答boy或者girl时候,一共能循环五次,回答的结果不是boy/girl时候,出现“what do you mean?"然后游戏直接结束。

我目前跑出来的结果是回答girl或者非boy/girl时,结果和我期望的一样。但回复boy时候,会出现
”you are a male."
"what do you mean?"
然后游戏结束。。。

为什么回答boy后出现这种情况呢?怎么改才对呢。。百思不解

isdkz 发表于 2023-10-11 18:24:30

你的代码中有一个小错误。在输入不是"boy"或"girl"时,你使用了break语句来跳出循环,这将导致循环永远只执行一次。你需要将break移动到else语句的外面,这样循环才能在计数器减少到0之前正常运行。以下是修改后的代码:

counts = 5

while counts > 0:
    gender = input("what is your gender?")

    if gender.lower() == "boy":
      print("you are a male.")
    elif gender.lower() == "girl":
      print("you are a female.")
    else:
      print("what do you mean?")
      break

    counts -= 1


注意,我使用了elif语句来检查用户输入的性别是否为 "girl",这样可以避免每次循环都检查两次用户输入。另外,我将计数器的减少移到了循环的末尾,这样可以确保每次循环都会减少计数器。

陶远航 发表于 2023-10-11 18:24:42

你的代码逻辑有一点小问题。在你的代码中,你是在每次输入\"boy\"或者\"girl\"后减少计数,而在输入其他内容时直接跳出循环。这样的话,如果输入的是\"boy\"或者\"girl\",程序会正常运行,但是如果输入的不是这两个选项,程序会直接结束,而不是提示\"what do you mean?\"。

你应该将\"else\"语句放在\"if\"语句的外面,这样无论输入什么,只要不是\"boy\"或者\"girl\",就会提示\"what do you mean?\"并结束游戏。这样修改后的代码如下:

counts = 5
while counts > 0:   
    gender = input(\"what is your gender?\")
    if gender.lower() == \"boy\":         
      print(\"you are a male.\")
      counts -= 1
    elif gender.lower() == \"girl\":         
      print(\"you are a female.\")
      counts -= 1
    else:         
      print(\"what do you mean?\")
      break


这样,当输入\"boy\"或者\"girl\"时,会打印相应的消息,并且计数器会减1,当输入其他内容时,会打印\"what do you mean?\"并结束游戏。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案

Mike_python小 发表于 2023-10-11 18:25:44

你的代码逻辑有一些小问题,修改后的代码如下:


counts = 5
while counts > 0:
    gender = input("What is your gender? ")
    if gender.lower() == "boy":
      print("You are a male.")
      counts -= 1
    elif gender.lower() == "girl":
      print("You are a female.")
      counts -= 1
    else:
      print("What do you mean?")
      break


在原代码中,counts应该在回答正确时减少。因此,在正确的条件语句下,应该将counts -= 1放在if或者elif的内部。此外,需要使用elif来判断是否为"girl",以避免重复执行判断。

修改后的代码将会循环五次,每次要求输入性别,并根据回答给出相应的输出。如果回答不是"boy"或"girl",则会输出"What do you mean?"并结束游戏。

希望对你有所帮助!如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:}

ba21 发表于 2023-10-11 18:34:34

counts=5

while counts>0:
    gender=input("what is your gender?")

    if gender.lower()=="boy":
      print("you are a male.")
      
    elif gender.lower()=="girl":
      print("you are a female.")      
      
    else:
      print("what do you mean?")
      break
   
    counts=counts-1

Menlo 发表于 2023-10-11 18:42:13

Mike_python小 发表于 2023-10-11 18:25
你的代码逻辑有一些小问题,修改后的代码如下:



请问,为什么用我的代码在回复boy时候,会出现
”you are a male."
"what do you mean?"
然后游戏结束

呢?

琅琊王朝 发表于 2023-10-11 21:46:38

因为gender.lower()=="boy",所以
print("you are a male.")
print("what do you mean?")

smallwh 发表于 2023-10-14 08:09:22

出现的问题:如果输入 boy,第一个 if 语句成立,执行 print("you are a male.") 。第二个 if 语句不成立,执行 else 里面的 print("what do you mean?") 。这里的第二个 if 和 else 是一对,第一个 if 与其无关。所以第一个 if 成立与否,不影响 else 代码块的执行; else 代码块是由第二个 if 决定的。
修改后的代码:counts = 5

while counts > 0:
    gender=input("what is your gender?")

    if gender.lower() == "boy":
      print("you are a male.")   
    elif gender.lower() == "girl":
      print("you are a female.")   
    else:
      print("what do you mean?")
      break

    counts=counts-1
改动:1.将第二个 if 改为 elif 。 if-elif-else 相当于:
if gender.lower() == "boy":
    print("you are a male.")
else:
    if gender.lower() == "girl":
      print("you are a female.")   
    else:
      print("what do you mean?")
2.无论输入 boy 还是 girl , counts 都应该 -1

yinda_peng 发表于 2023-10-14 09:29:07

悬挂else问题,小甲鱼在课上应该讲了的吧

hao7608 发表于 2023-10-14 09:35:30

把girl前面的if改为elif,把counts -= 1已到if循环外面就可以了,代码如下:
counts=5

while counts>0:
    gender=input("what is your gender?")

    if gender.lower()=="boy":
      print("you are a male.")

    elif gender.lower()=="girl":
      print("you are a female.")
      
    else:
      print("what do you mean?")
      break
   
    counts=counts-1

360341024 发表于 2023-10-17 10:10:49

第二个if已经和最底下的else最成了一对判断语句,当你运行程序,会先判断第一个if,然后会继续执行下一个if -else,所以就出现了你说的问题,正确是把第二个if改成elif{:10_256:}{:10_256:}

kerln888 发表于 2023-10-17 11:01:05

上面说把break移到外面的。。。代码不都还是在里面吗??

eat蟒蛇 发表于 2023-10-17 16:55:20

主要有三处修改:
1、把接收键盘的输入放到循环外,只接收一次即可!
2、把循环条件放到分支外面,统一处理。
3、修改判断的逻辑,使用嵌套if-else才能达到你的效果。

counts=5
gender=input("what is your gender?")   # 1
while counts>0:
    counts -=1                                     # 2
    if gender.lower()=="boy":
      print("you are a male.")
    else:                                              # 3
       if gender.lower()=="girl":
         print("you are a female.")
          # counts=counts-1
      
       else:
         print("what do you mean?")
         break
   

朕也来学学 发表于 2023-10-18 21:48:24


counts=5
gender=input("what is your gender?")   # 1
while counts>0:
    counts -=1                                     # 2
    if gender.lower()=="boy":
      print("you are a male.")
    else:                                              # 3
       if gender.lower()=="girl":
         print("you are a female.")
          # counts=counts-1
      
       else:
         print("what do you mean?")
         break
   

朕也来学学 发表于 2023-10-18 21:50:31


counts=5
gender=input("what is your gender?")   # 1
while counts>0:
    counts -=1                                     # 2
    if gender.lower()=="boy":
      print("you are a male.")
    else:                                              # 3
       if gender.lower()=="girl":
         print("you are a female.")
          # counts=counts-1
      
       else:
         print("what do you mean?")
         break
   

harryhan123 发表于 2023-10-21 14:16:39


counts=5
gender=input("what is your gender?")   # 1
while counts>0:
    counts -=1                                     # 2
    if gender.lower()=="boy":
      print("you are a male.")
    else:                                              # 3
       if gender.lower()=="girl":
         print("you are a female.")
          # counts=counts-1
      
       else:
         print("what do you mean?")
         break
   

flyqianhai 发表于 2023-10-22 09:39:30

你们都是大神,这个python,我还不太会,有点尴尬
页: [1]
查看完整版本: Python,萌新求助,超简单问题,给鱼币