免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
1 [报告]
发表于 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
2 [报告]
发表于 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
3 [报告]
发表于 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
4 [报告]
发表于 2009-02-04 16:25 |显示全部楼层

我疏忽了。。while循环确实不需要thanks

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."
      
    else:
        print "The binary reprensentation of " + str(num) + "(10) has even parity."

论坛徽章:
0
5 [报告]
发表于 2009-02-05 09:20 |显示全部楼层
原帖由 cc520 于 2009-2-4 17:33 发表
import sys
while 1:
    num=0
    dd={0:"even",1:"odd"}
    t=raw_input("Please input a nonnegative integer:  Q(Exit) ")
    if t=="Q" or t=="q":break
    if t.isdecimal():
        num=int ...

你使用的是什么版本啊 我运行了下
提示error
AttributeError: 'str' object has no attribute 'isdecimal'

不过你的思路比我清晰,我都不会用词典 - -#
另外好像不需要 import sys的吧?

论坛徽章:
0
6 [报告]
发表于 2009-02-05 11:04 |显示全部楼层
原帖由 作业没做完 于 2009-2-5 09:42 发表


不是,高度是变量 在0.5英寸和40英尺之间的数

15楼同学的用LIST存是啥意思?

我确实是菜鸟我只学了2个多礼拜 简明python还没看玩>.<
不过我追求完美 这个是最终改良版 我的是2.6的,一起学习 呵呵
import string
while 1:
    dd={0:"even",1:"odd"}
    try:
        t=str(raw_input("Please input a nonnegative integer:  Q(Exit) "))
    except:
        print "\ninput error\n"
        t=str(raw_input("Please input a nonnegative integer:  Q(Exit) "))
    if t=="Q" or t=="q":
        break
    elif t.isdigit()==0:
        print " %s is not even or odd"%t
        continue
    elif t.isdigit():
        num=int(t)
    else:
        print "%s is not integer"%num
        continue
    print "this number %s is %s"%(num,dd[num%2])

论坛徽章:
0
7 [报告]
发表于 2009-02-05 12:58 |显示全部楼层
原帖由 作业没做完 于 2009-2-5 09:42 发表


不是,高度是变量 在0.5英寸和40英尺之间的数

15楼同学的用LIST存是啥意思?


这样搞就可以replace替换,你把完整题目发上来我才能完整的解决,大概就是这个思路,还有字典也可以。
import string
num=str(raw_input("please some number"))
a=str(num)
a=a.replace('1', 'one')
a=a.replace('2', 'two')
a=a.replace('3', 'three')
a=a.replace('4', 'four')
a=a.replace('5','fire')
a=a.replace('6', 'six')
a=a.replace('7', 'seven')
a=a.replace('8', 'eight')
a=a.replace('9', 'nine')
a=a.replace('0', 'zero')
print a

字典的我只简单说下大概思路
a={1:'one',2:'two'}你还可以添加3~9
b=多少英尺
c=多少英寸
然后那个转换英尺 英寸的公式我不知道,但是一定有个结果,你自己用函数取得他的整数和小数部分,然后 integer一下
然后你用如
print "%s英尺 %s英寸"%( a[中括号b中括号] , a[c]   ) 的格式就可以自由转换了

晕死 论坛[中括号b中括号] 会变成加粗

你把题目完整发上来我才能写出完整例子

[ 本帖最后由 zhenglxd 于 2009-2-5 13:01 编辑 ]

论坛徽章:
0
8 [报告]
发表于 2009-02-05 13:57 |显示全部楼层
原帖由 作业没做完 于 2009-2-5 13:35 发表


兄弟。。你这个有点复杂。
我的最终版在上面已经写出来了,很短
不过我那个每测一次就要按一次F5
我把你这个存下来学习哈=v=~

另外LIST代码的那个!我有思路了~我回家先试试~
不行俺再上来求教哈~~谢 ...

呵呵 一起努力 我也是刚学习的 所以写的代码不是很规范 人人为我 我为人人么!

论坛徽章:
0
9 [报告]
发表于 2009-02-06 11:16 |显示全部楼层

回复 #23 作业没做完 的帖子

metres = float(raw_input("Enter the height (in metres): "))
a = {1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',\
     7:'seven',8:'eight',9:'nine',10:'ten',20:'twenty',\
     30:'thirty',40:'forty',50:'fifty',60:'sixty',70:'seventy',\
     80:'eighty',90:'ninety',100:'hundred',\
     1000:'thousand '}
T=total_inches = 39.37 * metres
F=feet = (int(T//12))
SF=str(F)
I=round(float(T/12)-F,1)
II=int(I*10)
if len(SF)==1:
    print "It is " + str(a[F])+" feet, " + str(a[II]) + ' inches high.'
if len(SF)==2:
    L=F-F//10*10
    print "It is " + str(a[F//10*10])+str(a[L]) + "  feet,"+\
          str(a[II]) + ' inches high.'
if len(SF)==3:
    H=F//100
    TEN=(F-F//100*100)//10*10
    OO=(F-F//100*100)-TEN

    print "It is " + str(a[H])+'  hundred'+"  "+str(a[TEN])+" "+a[OO]+\
          "  feet,"+ str(a[II]) + ' inches high.'
         
         
         
我刚学没多久 我这个是比较笨的,我所学的知识中我只知道这样子是可以实现你那个功能的,这仅仅是一种思路。注意11~19我忘记添加了疏忽了。所以遇到11的时候可能会打出tenone你自己改下就可以了,但理论上你可以用他稍微修改可以做出无限的数字,当然计算机的处理能力是有限的实际中你也用不大那么大的数字,
其中 我的思路就是用len() 函数来判断 这个数字是个几位数?
如果你有兴趣不怕烦 你可以自己跟着这个思路做到 千万位,亿万位,当然词典要记得添加一下关键字,重复性的用公式把他分化掉
我的思路就是比如说 11111就= 1万+1千+1百+十一,这个至少比 你在词典从1~11111要轻松不是么?
希望能帮助到你,共同进步吧
PS写这个东东花了我2个多小时 另我英语基础很差 我不知道老外表达 数字时 是不是这样表达的!

论坛徽章:
0
10 [报告]
发表于 2009-02-06 12:26 |显示全部楼层

回复 #25 作业没做完 的帖子

呵呵,其实都是新人不用怎么客气,我所学习的一切资源来源于网络,我能想到仅仅是因为我学过这个例子,如果你也学过那么你也可以,其实遇到问题,才能反映出你在哪些方面还有欠缺,我也很感谢你把这个问题分享出来,我在帮你解决问题的过程中,也等于把我自己的知识点复习了一遍,并且是实践的,我相信总有一天我们也会脱离菜鸟的,共同努力吧!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP