免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 9685 | 回复: 39
打印 上一主题 下一主题

菜鸟又来问问题咯~TAT [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-04 10:41 |只看该作者 |倒序浏览
是要写一个关于 检查数字奇偶性 的小程序
要用到WHILE LOOP 和 IF STATEMENT。

num = int(raw_input("Please input a nonnegative integer: "))

r = ""
while r != 0:
    r = num%2
    if r == 1:
        print "The binary reprensentation of " + str(num) + "(10) has odd parity."
    elif r != 0:
        r1 = num%2
        num1 = num/2
   
print "The binary reprensentation of " + str(num) + "(10) has even parity."
   


请问这段代码哪里有错?

因为我检查奇数的时候程序就无限的打出 "The binary reprensentation of " + str(num) + "(10) has odd parity." 这一段。

多谢帮忙!

论坛徽章:
0
2 [报告]
发表于 2009-02-04 10:46 |只看该作者
同学你好
留学海外的吧?
while循环没有退出所以不停的打印
直接删掉while那一行就好了
貌似不需要while

论坛徽章:
0
3 [报告]
发表于 2009-02-04 11:03 |只看该作者

我也是菜鸟我对你的功课很感兴趣啊!!

while True:
    num = int(raw_input("Please input a nonnegative integer: "))
    if num==0:
        print 'error'
    elif num%2==1:
        print "The binary reprensentation of " + str(num) + "(10) has odd parity."
        break
    else:
        print "The binary reprensentation of " + str(num) + "(10) has even parity."
        break
   
我也是菜鸟,你还有其他功课的话都发上来啊,我很感兴趣。这个是我自己做的。

论坛徽章:
0
4 [报告]
发表于 2009-02-04 11:11 |只看该作者
原帖由 作业没做完 于 2009-2-4 10:41 发表
是要写一个关于 检查数字奇偶性 的小程序
要用到WHILE LOOP 和 IF STATEMENT。

num = int(raw_input("Please input a nonnegative integer: "))

r = ""
while r != 0:
    r = num%2
    if r == 1:
...

你的程序里
r应该是个开关吧
你没有用break去中断 所以当满足循环条件的时候就会打印出"The binary reprensentation of " + str(num) + "(10) has odd parity."然后再次去循环再次打印出重复的东西

论坛徽章:
0
5 [报告]
发表于 2009-02-04 11:16 |只看该作者
谢谢同学
呃。。我是啊~~

但是题目要求要WHILE啊。。5555

论坛徽章:
0
6 [报告]
发表于 2009-02-04 11:26 |只看该作者
break 换成 continue

论坛徽章:
0
7 [报告]
发表于 2009-02-04 11:44 |只看该作者
原帖由 zhenglxd 于 2009-2-4 11:03 发表
while True:
    num = int(raw_input("Please input a nonnegative integer: "))
    if num==0:
        print 'error'
    elif num%2==1:
        print "The binary reprensentation of " + str(num) ...



好的好的 没有问题……
我每个星期都会有问题的STO。。。。当然希望我问题越来越少 能自己解决就好了TAT~~

谢谢你啊!

论坛徽章:
0
8 [报告]
发表于 2009-02-04 12:55 |只看该作者
原帖由 作业没做完 于 2009-2-4 11:44 发表



好的好的 没有问题……
我每个星期都会有问题的STO。。。。当然希望我问题越来越少 能自己解决就好了TAT~~

谢谢你啊!

问题是无止尽的,关键是找到解决问题的方法和思路

论坛徽章:
0
9 [报告]
发表于 2009-02-04 14:20 |只看该作者

我又改进了下!嘿嘿

import sys
while True:
    try:
        num = int(raw_input("Please input a nonnegative integer: "))
    except ValueError:
        print 'error'
        continue
    if num==0:
        print 'error'
    elif num%2==1:
        print "The binary reprensentation of " + str(num) + "(10) has odd parity."
        continue
    else:
        print "The binary reprensentation of " + str(num) + "(10) has even parity."
        continue

论坛徽章:
0
10 [报告]
发表于 2009-02-04 14:52 |只看该作者
原帖由 zhenglxd 于 2009-2-4 14:20 发表
if num==0:
        print 'error'
    elif num%2==1:
        print "The binary reprensentation of " + str(num) + "(10) has odd parity."
        continue
    else:
        print "The binary reprensentation of " + str(num) + "(10) has even parity."
        continue


这么多continue有必要么?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP