鱼C论坛

 找回密码
 立即注册
查看: 1333|回复: 6

[已解决]for循环和if语句的嵌套

[复制链接]
发表于 2018-5-26 14:32:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 一次芳 于 2018-5-26 15:34 编辑

我的源程序:
names=['mother','teacher','student','father']
identity=input("please entry your identity:")
if identity not in names:
    print("unidentifiable, systen not record your identity")
else:
    for name in names:
        if identity==name:
            print("you are a "+identity.title()+",today you should clean honse.")
        elif identity==name:
            print("you are a "+identity.title()+",today you should standy.")
        elif identity==name:
            print("you are a "+identity.title()+",today you should teach your student.")
        else:
            print("you are a "+identity.title()+",today you should work.")
   
我的运行结果:
please entry your identity:student
you are a Student,today you should work.
you are a Student,today you should work.
you are a Student,today you should clean honse.
you are a Student,today you should work.

问题:不是应该输出   you are a Student,today you should standy   
万能的鱼友帮个忙
最佳答案
2018-5-26 14:49:25
本帖最后由 thexiosi 于 2018-5-26 14:55 编辑

hi 原因如下

  1. names=['mother','teacher','student','father']
  2. identity=input("please entry your identity:")
  3. if identity not in names:
  4.     print("unidentifiable, systen not record your identity")
  5. else:
  6.     #for name in names:# here 此处用for循环,引发逻辑混乱。需要进行修正
  7.     print(identity)
  8.     if identity==names[0]:
  9.         print("you are a "+identity.title()+",today you should clean honse.")
  10.     elif identity==names[2]:
  11.         print("you are a "+identity.title()+",today you should standy.")
  12.     elif identity==names[1]:
  13.         print("you are a "+identity.title()+",today you should teach your student.")
  14.     else:
  15.         print("you are a "+identity.title()+",today you should work.")
复制代码


please entry your identity:student
student
you are a Student,today you should standy.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-5-26 14:49:25 | 显示全部楼层    本楼为最佳答案   
本帖最后由 thexiosi 于 2018-5-26 14:55 编辑

hi 原因如下

  1. names=['mother','teacher','student','father']
  2. identity=input("please entry your identity:")
  3. if identity not in names:
  4.     print("unidentifiable, systen not record your identity")
  5. else:
  6.     #for name in names:# here 此处用for循环,引发逻辑混乱。需要进行修正
  7.     print(identity)
  8.     if identity==names[0]:
  9.         print("you are a "+identity.title()+",today you should clean honse.")
  10.     elif identity==names[2]:
  11.         print("you are a "+identity.title()+",today you should standy.")
  12.     elif identity==names[1]:
  13.         print("you are a "+identity.title()+",today you should teach your student.")
  14.     else:
  15.         print("you are a "+identity.title()+",today you should work.")
复制代码


please entry your identity:student
student
you are a Student,today you should standy.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-5-26 15:03:08 | 显示全部楼层
本帖最后由 一次芳 于 2018-5-26 15:14 编辑


elif结构的选择语句,当碰到第一个合法的条件时,剩下的条件便不会再判断了,那循环到name为student时不是只应该打印you are a student ,today you should clean house吗?
试了一下还是不行
改了之后结果是这样的
please entry your identity:student
you are a Student,today you should work.
you are a Student,today you should work.
you are a Student,today you should work.
you are a Student,today you should work.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-5-26 15:06:06 | 显示全部楼层
这么不合理的代码。。。。。。。

假设你identity ="student"   name = "student"

if identity==name: #输出
            print("you are a "+identity.title()+",today you should clean honse.")
        elif identity==name: #输出
            print("you are a "+identity.title()+",today you should standy.")
        elif identity==name: #输出
            print("you are a "+identity.title()+",today you should teach your student.")
        else:
            print("you are a "+identity.title()+",today you should work.")

上面 if identity==name elif  identity==name 都一样的条件,还elif干嘛?多此一举。

修改你的代码

  1. names=['mother','teacher','student','father']
  2. identity=input("please entry your identity:")

  3. if identity not in names:
  4.     print("unidentifiable, systen not record your identity")
  5. else:
  6.     for name in names:
  7.         if identity==name:
  8.             print("you are a "+identity+",today you should clean honse.")
  9.             break
  10.         if identity==name:
  11.             print("you are a "+identity+",today you should standy.")
  12.             break
  13.         if identity==name:
  14.             print("you are a "+identity+",today you should teach your student.")
  15.             break
  16.         if identity==name:
  17.             print("you are a "+identity+",today you should work.")
  18.             break
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-5-26 15:20:20 | 显示全部楼层
本帖最后由 一次芳 于 2018-5-26 15:25 编辑
ba21 发表于 2018-5-26 15:06
这么不合理的代码。。。。。。。

假设你identity ="student"   name = "student"


谢谢哈,但是改了之后结果依然不是我想要的。。。
我输入student   应该打印的是 you are a student,today you should stanty
但是按照你的修改,输出的是you are a student,today you should clean honose,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-5-26 15:33:31 | 显示全部楼层
感谢各位,我已经找到原因了
names=['mother','teacher','student','father']
identity=input("please entry your identity:")
if identity not in names:
    print("unidentifiable, systen not record your identity")
else:
    if names[0]==identity:
            print("you are a "+identity.title()+",today you should clean honse.")
    elif names[1]==identity:
            print("you are a "+identity.title()+",today you should standy.")
    elif names[2]==identity:
            print("you are a "+identity.title()+",today you should teach your student.")
    else:
            print("you are a "+identity.title()+",today you should work.")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-5-26 15:36:55 | 显示全部楼层
一次芳 发表于 2018-5-26 15:33
感谢各位,我已经找到原因了
names=['mother','teacher','student','father']
identity=input("p ...

这不是闲的吗,直接字符串判断啊。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-17 14:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表