首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >组织问题?

组织问题?
EN

Stack Overflow用户
提问于 2014-03-17 01:59:58
回答 5查看 182关注 0票数 0

基本上,我做这个程序是为了练习python (我是一个完全的新手),我非常喜欢python,因为我学过的第一个编程语言,或者在完成一个可以工作的程序的过程中,我感到非常有成就感(即使它是hello world)。所以不管怎样,我用从书本和网上学到的技术做了一个小程序,我有一个问题,程序运行得很好,没有问题,但最后有一个除法,它只是出了问题,它不能除以任何东西,除非它使一个整数(例如。100/20=5,但如果我做20/100,它将等于0,而不是0.2),这也会影响它,如果数字将是负的,它只是恐慌。我尝试了15/20,看看它是否四舍五入,但它仍然显示为0。任何帮助都将是非常棒的^_^这里是代码:

代码语言:javascript
复制
a=100
b=50
c=10
z=110
o=5
zoo=z+o+o

print "What is the value of zoo if:"
print "z=110"
print "o=5"
print "zoo=z+o+o"
import time
time.sleep(5)
print zoo,"of course!"

import time
time.sleep(1)

print "Wait..",a+b-(c)*3,"is the same as zoo except we just did it there using other code!"
import time
time.sleep(3)
print "We did it using 100+50-(10)*3 which then adds to zoo or 120!"

import time
time.sleep(3)

print "were gonna try something fun now!"
import time
time.sleep(2)

print "Please pick a number:"
number=int(raw_input())

print "and another:"
another=int(raw_input())

print "the two numbers you chose multiplied together makes",number*another
import time
time.sleep(2)
print "ok now were going to take your two numbers and divide them"
print "Your two numbers divided=",number/another
import time
time.sleep(1)
print "Ok im bored now, im going to go, have a nice day ^_^"

这是一个有问题的awnser:

代码语言:javascript
复制
What is the value of zoo if:
z=110
o=5
zoo=z+o+o
120 of course!
Wait.. 120 is the same as zoo except we just did it there using other code!
We did it using 100+50-(10)*3 which then adds to zoo or 120!
were gonna try something fun now!
Please pick a number:
15
and another:
20
the two numbers you chose multiplied together makes 300
ok now were going to take your two numbers and divide them
Your two numbers divided= 0
Ok im bored now, im going to go, have a nice day ^_^

哦,我在python2.7.6上

EN

回答 5

Stack Overflow用户

发布于 2014-03-17 02:04:57

在该行上方添加:

代码语言:javascript
复制
print "Your two numbers divided=",number/another

这段代码:

代码语言:javascript
复制
number, another = number + .0, another + .0

您的代码不能工作的原因是您使用的是int's。当你除以整数时,它们返回一个整数或整数。您需要通过将.0添加到数字来将数字转换为floats。这将允许您获得绝对除法结果。

票数 0
EN

Stack Overflow用户

发布于 2014-03-17 02:05:12

执行整数除法时为15/20 = 0,因为结果小于1。因此,它被截断为0

//用于整型除法,/用于浮点型除法-您使用了错误的运算符,因此得到了错误的结果:

代码语言:javascript
复制
>>> 15 / 20
0
>>> 15 // 20
0.75

您可以通过将from __future__ import division中的内容添加到脚本来修复此问题。当使用/运算符时,这将始终执行float除法,并使用//进行整数除法-因此,只要执行您正在做的操作,它就会返回预期的结果:

代码语言:javascript
复制
>>> from __future__ import division
>>> 15 / 20
0.75

对于import,我将使用上面的解决方案;但还有其他方法。另一种选择是将至少一个操作数设为float,例如float(number) / another

代码语言:javascript
复制
>>> number = 15
>>> another = 20
>>> float(number) / another
0.75

上面的方法之所以有效,是因为在Python中,除法的结果取决于所使用的值类型。

票数 0
EN

Stack Overflow用户

发布于 2014-03-17 02:06:51

您可以添加

代码语言:javascript
复制
from __future__ import division

在你文件的顶部。那么默认的除法策略将是您所期望的,即浮点除法。Python 2.7默认做整数除法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22440619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档