注册 登录
编程论坛 Python论坛

在这里向python大神们求教一个小问题

f2001927 发布于 2019-08-06 10:04, 2800 次点击

我最近在研究python语言,在看完mooc上的温度转换的python实力后,想结合自己工作情况编写一个python语言
因为本人是从事路桥方向所以主要是涉及土方的填挖事情。
程序代码:
#TempConvert.property
TempStr = input("请输入带有符号的挖土方工程数量:")
if TempStr[-1] in ['W','w']:
    C = (eval(TempStr[0:-1]) )
    print("挖普通土{:.2f}方".format(C))
else:    print("输入格式错误")

这个是我自己在温度转换的代码上进行修改的,因为运行起来总是提示错误。
我的理想状态就是
录入:12普通土
然后就在下面显示:“请输入带有符号的挖土方工程数量:12方普通土”
请指点下我这个语言错误的地方啊
只有本站会员才能查看附件,请 登录

我想问下能不能将以上这个语言修改为我录入的内容是“12方或12m3”啊?

[此贴子已经被作者于2019-8-6 10:09编辑过]

15 回复
#2
f20019272019-08-06 10:11
只有本站会员才能查看附件,请 登录
#3
wp2319572019-08-06 10:45
回复 2楼 f2001927
说说你想实现的功能,eval 不是那么用的
#4
f20019272019-08-06 10:51
回复 3楼 wp231957
分别提示录入普通土的数量、松土、硬土的数量
这时候录入数量的习惯有以下三种:“12方”或“12m3”、“12m3”这三种情况
最终显示结果就是“挖土方数量=普通土+松土+硬土”
挖石方这块我自己研究摸索
只有本站会员才能查看附件,请 登录
#5
f20019272019-08-06 10:53
回复 3楼 wp231957 关于挖方土方的计算
只有本站会员才能查看附件,请 登录

逻辑关系如下:
挖方=挖土方+挖石方
挖土方=挖普通土+挖松土+挖硬土
的合计
挖石方我自己通过挖土方的方式自己修改下。
#6
wp2319572019-08-06 10:55
回复 4楼 f2001927
你还是没说明白
比如 你输入12方  想让程序输出神马
比如你输入30M3 你想让程序输出神马
#7
f20019272019-08-06 10:59
回复 6楼 wp231957
录入“12方”或“12m3”或“12m3”
经过python处理后直接显示12方即可
#8
wp2319572019-08-06 11:02
回复 7楼 f2001927
>>> tempstr = input("请输入带有符号的挖土方工程数量:"))
请输入带有符号的挖土方工程数量:12方
>>> print(tempstr)
12方
>>>
#9
wp2319572019-08-06 11:08
回复 7楼 f2001927
tempstr = input("请输入带有符号的挖土方工程数量:")
if tempstr[-1:]=="方" :
    print(tempstr[0:-1],"方")
elif  tempstr[-2:]=="m3"  or tempstr[-2:]=="M3" :
    print(tempstr[0:-2],"方")
else:
    print("error")   
#10
wp2319572019-08-06 11:09
回复 7楼 f2001927
e:\pytest>python ex28.py
请输入带有符号的挖土方工程数量:12方
12 方

e:\pytest>python ex28.py
请输入带有符号的挖土方工程数量:12m3
12 方

e:\pytest>python ex28.py
请输入带有符号的挖土方工程数量:舒服撒
error
#11
f20019272019-08-06 11:26
回复 10楼 wp231957
[em19谢谢版主。
兄弟刚学这个东西没多久,很多地方不明白希望可以通过QQ或其他方式可以跟您取得联系啊。方便的话私信下您QQ号码可以吗?
#12
f20019272019-08-06 11:34
回复 9楼 wp231957
程序代码:
tempstr = input("挖普通土:")
if tempstr[-1:]=="" :
    print(tempstr[0:-1],"")
elif  tempstr[-2:]=="m3"  or tempstr[-2:]=="M3"   or tempstr[-2:]=="m3" :
    print(tempstr[0:-2],"")
else:
    print("error")   
tempstr = input("挖软土:")
if tempstr[-1:]=="" :
    print(tempstr[0:-1],"")
elif  tempstr[-2:]=="m3"  or tempstr[-2:]=="M3"   or tempstr[-2:]=="m3"  :
    print(tempstr[0:-2],"")
else:
    print("error")
tempstr = input("挖硬土:")
if tempstr[-1:]=="" :
    print(tempstr[0:-1],"")
elif  tempstr[-2:]=="m3"  or tempstr[-2:]=="M3"   or tempstr[-2:]=="m3"  :
    print(tempstr[0:-2],"")
else:
    print("error")

版主,您好!我在您给我的代码基础上我分别进行微调了下。以上代码是可以将挖“普通土、软土、硬土”的数量对应出来
如图所示
只有本站会员才能查看附件,请 登录

我现在有一个问题就是如何显示出来
“挖土方=34方”的数量显示出来?
显示结果这样→“挖土方=挖普通土+挖软土+挖硬土[34方]”
#13
wp2319572019-08-06 12:04
回复 12楼 f2001927
程序代码:

t1=t2=t3=0
tempstr = input("挖普通土:")
if tempstr[-1:]=="" :
    t1=int(tempstr[0:-1])   #确保是int类型输入 而不是浮点型输入
elif  tempstr[-2:]=="m3"  or tempstr[-2:]=="M3"   or tempstr[-2:]=="m3" :
    t1=int(tempstr[0:-2])   #确保是int类型输入 而不是浮点型输入
else:
    print("error")   
tempstr = input("挖软土:")
if tempstr[-1:]=="" :
    t2=int(tempstr[0:-1])   #确保是int类型输入 而不是浮点型输入
elif  tempstr[-2:]=="m3"  or tempstr[-2:]=="M3"   or tempstr[-2:]=="m3"  :
    t2=int(tempstr[0:-2])   #确保是int类型输入 而不是浮点型输入
else:
    print("error")
tempstr = input("挖硬土:")
if tempstr[-1:]=="" :
    t3=int(tempstr[0:-1])   #确保是int类型输入 而不是浮点型输入
elif  tempstr[-2:]=="m3"  or tempstr[-2:]=="M3"   or tempstr[-2:]=="m3"  :
    t3=int(tempstr[0:-2])   #确保是int类型输入 而不是浮点型输入
else:
    print("error")
   
print("总土方量: %d 方"%(t1+t2+t3))   


'''
e:\>python ex3.py
挖普通土:12m3
挖软土:45方
挖硬土:56M3
总土方量: 113 方

e:\>
'''
#14
wp2319572019-08-06 12:05
以下是引用f2001927在2019-8-6 11:26:16的发言:

[em19谢谢版主。
兄弟刚学这个东西没多久,很多地方不明白希望可以通过QQ或其他方式可以跟您取得联系啊。方便的话私信下您QQ号码可以吗?

我不咋用qq的
#15
f20019272019-08-06 19:42
回复 13楼 wp231957
您好,我直接将您的代码复制过来运行后,我这边的显示怎么是这样啊???
只有本站会员才能查看附件,请 登录
#16
wp2319572019-08-06 21:21
回复 15楼 f2001927
你应该使用自己的ide而不是使用在线的
1