简体   繁体   English

如何在Python 3.3.2中读取浮点数

[英]How to read in floating-point numbers in Python 3.3.2

I just start with python in school, Im not good but I will try to explain my best. 我刚从学校的python开始,我不好,但我会尽力解释我的最好。 I have a program and I want the program to accept decimals insted of just integers. 我有一个程序,我希望程序接受只有整数的小数。 I dont know how. 我不知道怎么做。 and can you help me to improve my code? 你能帮我改进我的代码吗? Sorry for my english.. A part of the code is swedish but i think you can understand. 对不起我的英文..代码的一部分是瑞典语,但我想你可以理解。 If you dont just ask me! 如果你不问我! Best regards Jesper 最好的问候Jesper

a = int(input("Skriv ett heltal (ej decimaltal): "))        
b = int(input("Skriv ett till heltal (ej decimaltal): ")) 
print()
print('Kvadrattabell')
print('=============')                      
print('Tal\tKvadrat') 

for i in range(a, b+1):
print("{0}\t{1}".format(i,i*i))

def funktion():
    a = int(input("Skriv ett heltal: "))         
    b = int(input("Skriv ett till heltal: ")) 
for i in range(a, b+1):                      
    print("{0}\t{1}".format(i,i*i))  
while True: 
    svar = input('Vill du köra programmet igen? (J/N) ') 
    if svar == 'J' or svar == 'j'or svar == "Jajemensan" or svar == "Ja" or svar == "Ja! Gör det": 
    funktion() 
if svar == 'N' or svar == 'n' or svar == 'Nej' or svar == "Nejtack" or svar == "Det räcker för idag!": 
    break
print('Ok då. Tack för idag!') 

First, integers cannot contain decimals. 首先,整数不能包含小数。 If you want to have decimals, you'll have to use float types! 如果你想要小数,你将不得不使用浮点类型!

Use the float() function: 使用float()函数:

a = float(input("Skriv ett heltal (ej decimaltal): "))        
b = float(input("Skriv ett till heltal (ej decimaltal): "))

And in your funktion function: 而在你funktion功能:

def funktion:
    a = float(input("Skriv ett heltal: "))         
    b = float(input("Skriv ett till heltal: "))

You can read more about types here Also, about your conditionals, you can make it simple by putting it to a list: 您可以在此处阅读有关类型的更多信息此外,关于您的条件,您可以通过将其放入列表来简化:

ja = ['j', 'jajemensan', 'ja' 'ja! gör det']
#lower it, and check if it's between the valid inputs:
if svar.lower() in ja:
    #do something

Hope this helps! 希望这可以帮助! If you need more help, just ask me :) 如果您需要更多帮助,请问我:)

You can replace int by float like this in every place where you want to accept input in your code:- 你可以在你希望接受代码输入的每个地方像这样用float替换int: -

 a = float(input("Skriv ett heltal (ej decimaltal): ")) 
 b = float(input("Enter another no: ")) 

similarly in other places. 同样在其他地方。 This is because, integers cannot accept decimals. 这是因为,整数不能接受小数。 For more information on built in types, you can check the following link http://www.tutorialspoint.com/python/python_variable_types.htm and http://docs.python.org/2/library/stdtypes.html 有关内置类型的更多信息,请查看以下链接http://www.tutorialspoint.com/python/python_variable_types.htmhttp://docs.python.org/2/library/stdtypes.html

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

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