简体   繁体   English

在Python中使用raw_input读取值

[英]Reading values with raw_input in Python

I am trying to do integration in Python but whenever I key in a value my outputs always results in 0. What the reason? 我正在尝试在Python中进行集成,但是每当我键入一个值时,我的输出总是结果为0。这是什么原因?

Eg: 例如:

def main():
   eq_of_form()

   value_of_a()
   value_of_b()
   value_of_c()
   value_of_m()
   value_of_n()

   value_of_x()
   area_under_graph()

def eq_of_form():
   print "Eq of the form y = ax^m + bx^n + c " + ":"

def value_of_a():
   return raw_input("Enter value for a \n")

def value_of_b():
   return raw_input("Enter value for b \n")

def value_of_c():
   return raw_input("Enter value for c \n")

def value_of_m():
   return raw_input("Enter value for m \n")

def value_of_n():
   return raw_input("Enter value for n \n")

def value_of_x():
   return raw_input("Enter a value for x to find " +
                 "value of y and the gradient at that point \n " + "x =   ")

def area_under_graph():
   y = (int(value_of_a())*int(value_of_x())**(int(value_of_m())+1))/((int(value_of_m())+1))
   // * 2nd part.This works for me(: 
   // + (int(value_of_b())) * (int(value_of_x())**
   // (int(value_of_n())+1))/(int(value_of_n())+1) + ((int(value_of_c())*int(value_of_x())))

   print y

main()

(* Note: the eq under the area_under_graph() function is only half of it because the other half kind of work so I did not post it:)) For the top code, I tried inputting the values here: (maybe you can try using the same(: ) (*注意: area_under_graph()函数下的eq仅是它的一半,因为另一半是工作,所以我没有发布它:))对于顶部代码,我尝试在此处输入值:(也许您可以尝试使用相同的(:)

a = 1
b = 2
c = 1
m = 2
n = 1
x = 1

I am supposed to get 7/3 which is 2.333, but I end up getting 2. The problem appears to lie in the first part of the eq. 我应该得到7/3,即2.333,但最终得到2。问题似乎出在等式的第一部分。

Sorry for the newbie question. 对不起,新手问题。

Your code at the start is wrong. 一开始您的代码是错误的。 You need to assign your variables after you read the user input: 阅读用户输入后,需要分配变量:

value_of_a()

should be: 应该:

a = value_of_a()

It is also unnecessary to write a separate function for inputting each variable. 也不必编写用于输入每个变量的单独函数。 You could instead have a function that takes a parameter: 您可以改用带有参数的函数:

def get_user_value(name):
    return raw_input("Enter value for %s\n" % name)

a = get_user_value("a")
b = get_user_value("b")
# etc..

But then you ignore all these values and read them again inside the area_under_curve() method. 但是随后您将忽略所有这些值,并在area_under_curve()方法中再次读取它们。 This is probably not what you intend to do. 这可能不是您打算做的。 Furthermore inside this method you assume that all parameters are integers. 此外,在此方法内部,您假定所有参数都是整数。 If you are using Python 2.5 the division here is integer division: 如果您使用的是Python 2.5,则此处的除法是整数除法:

m1/m2

This could return 0 when the result was actually supposed to be a non-integer like 0.125. 当结果实际上应该是非整数(如0.125)时,它可以返回0。 You need to use floats instead of integers to do the calculation. 您需要使用浮点数而不是整数来进行计算。 You can do this in Python 2.5 using float(m). 您可以在Python 2.5中使用float(m)进行此操作。 In Python 3.0 the division operator does what you want by default. 在Python 3.0中,除法运算符默认情况下会执行您想要的操作。

/ does Integer division in Python2, this means a/b is the biggest integer c with c*b <=a , so 7/3 is indeed 2 . /在Python2中执行Integer除法,这意味着a/bc*b <=a的最大整数c ,因此7/3的确是2 You want floats, so you need to use them .. replace all the int with float in your code. 您需要浮点数,因此需要使用它们..在代码中将所有int替换为float

You should probably take another look at functions too ... you code can be much much shorter :-) 您可能也应该再看一下函数...您的代码可以短很多:-)

In Python 2.x, dividing an integer by another integer results in an integer. 在Python 2.x中,将一个整数除以另一个整数将得出一个整数。 Either use from __future__ import division , or turn one of the integers into a float by passing it to float() . 可以from __future__ import division ,也可以将整数之一传递给float()将其转换为float()

The issue is that you're using integer arithmetic - see all those int calls you've got everywhere. 问题是您正在使用整数算术-查看到处都有的所有int调用。 Integer arithmetic (in Python 2.x) will only ever return integers, so you'll never get 2.33, only 2. 整数算术(在Python 2.x中)只会返回整数,因此您永远不会得到2.33,只有2。

Use float() instead of int() and things should work. 使用float()而不是int() ,事情应该可以进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM