如何在Python中接收格式化的数字输入。

huangapple go评论66阅读模式
英文:

how to receive formatted numbers inputs in python

问题

在Python中,如何以以下格式读取输入数字:

1,000,000
1,000.25
20%
20 %

我希望我的代码能理解用户输入:

1,000,000 等同于 1000000
1,000.25 等同于 1000.25

如果用户输入 25 = 25% = 25 %,都等同于 0.25

clname = input("输入您的姓名: ")
clloan = float(input("输入您的贷款金额: ").replace(',', ''))  # 修改此处以去除逗号
clrate = float(input("输入贷款利率 - 格式 0.000: "))
clyears = float(input("输入贷款期限(年): "))

rtval = clloan * clrate * clyears
total = clloan + rtval
monthlypayment = total / (clyears * 12)
print("**************************************************")
print("客户: ", clname)
print("贷款金额: ", clloan, " ,利率: ", clrate, " ,期限: ", clyears, " 年")
print("利率值为: ", rtval)
print("总还款额: ", total)
print("每月还款额: ", monthlypayment)

结果:

输入您的姓名: mm
输入您的贷款金额: 1000000
输入贷款利率 - 格式 0.000: 0.05
输入贷款期限: 5
**************************************************
客户:  mm
贷款金额:  1000000.0  ,利率:  0.05  ,期限:  5.0利率值为:  250000.0
总还款额:  1250000.0
每月还款额:  20833.333333333332
英文:

in python how to read input numbers in formats like:
1,000,000
1,000.25
20%
20 %

i want my code to understand if user put
1,000,000 equal 1000000
1,000.25 equal 1000.25

and if user put 25 = 25% = 25 % all equal to 0.25

clname=input("Enter Your Nanem: ")
clloan=float(input("Enter your loan value: "))
clrate=float(input("enter your loan rate - format 0.000 : "))
clyears=float(input("enter loan period in years: "))

rtval=clloan * clrate * clyears
total=clloan + rtval
monthlypayment=total / (clyears * 12)
print("**************************************************")
print("Client: ", clname)
print("a loan of: ",clloan , " ,at rate: ",clrate , " ,on period: " ,clyears," years")
print("rate value is: ", rtval)
print("total payment will be: ",total)
print("and your monthly payment will be: ",monthlypayment)

result:

Enter Your Nanem: mm
Enter your loan value: 1,000,000
Traceback (most recent call last):
  File "E:\pythonProjects\test1\VID4_exercise.py", line 22, in <module>
    clloan=float(input("Enter your loan value: "))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: could not convert string to float: '1,000,000'

Process finished with exit code 1

答案1

得分: 1

你可以使用 replace() 来替换在转换为浮点数之前的“,”和“%”。

clloan = float(input("请输入贷款金额:").replace(",", ""))
clrate = float(input("请输入贷款利率 - 格式为 0.000:").replace("%", ""))

如果担心多余的空格,也可以使用 replace(" ", "")。如果需要小数点值,还可以将百分比值除以100。

英文:

You can use replace() to replace the "," and "%" before converting them to float.

clloan=float(input("Enter your loan value: ").replace(",",""))
clrate=float(input("enter your loan rate - format 0.000 : ").replace("%",""))

If you are worried about extra space you can use replace(" ",""). You can divide the percentage value by 100 as well if you want decimal point values.

答案2

得分: 1

你可以采用不同的方法。

与其替换字符(例如逗号、百分号),考虑提取可能有意义的字符。然后尝试转换为浮点数。

类似这样:

NUMS = set('0123456789.-+')

def myFloat(s):
    sn = ''.join(c for c in s if c in NUMS)
    # 如果输入字符串没有意义或为空,可能会引发 ValueError
    rv = float(sn)
    return rv / 100 if s[-1] == '%' else rv

for value in '1,000,000', '-12.5', '15%':
    print(value, myFloat(value))

输出结果:

1,000,000 1000000.0
-12.5 -12.5
15% 0.15
英文:

You could take a different approach.

Rather than replacing characters (e.g., comma, percent) consider extracting only those characters that might possibly makes sense. Then try to convert to float.

Something like this:

NUMS = set('0123456789.-+')

def myFloat(s):
    sn = ''.join(c for c in s if c in NUMS)
    # there is potential for ValueError here if input string makes no sense or is empty
    rv = float(sn)
    return rv / 100 if s[-1] == '%' else rv

for value in '1,000,000', '-12.5', '15%':
    print(value, myFloat(value))

Output:

1,000,000 1000000.0
-12.5 -12.5
15% 0.15

答案3

得分: 0

Thank you for providing the code. Here's the translated part:

感谢您的回答我明白了

我使用了包含contains和替换replace来修复输入的数字下面是最终的解决方案

clname = input("输入您的姓名:")

clloan = input("输入您的贷款金额:")
clloan = float(clloan.replace('$', '').replace(',', '').replace(' ', ''))

clrate = input("输入您的贷款利率:")
clrate = float(clrate.replace('%', '').replace(',', '').replace(' ', '))

if clrate > 1:
    clrate = clrate / 100

clyears = float(input("输入贷款期限(年):"))

rtval = clloan * clrate * clyears
total = clloan + rtval
monthlypayment = total / (clyears * 12)

print("**************************************************")
print("客户姓名:", clname)
print("贷款金额:", clloan, ",利率:", clrate, ",期限:", clyears, "年")
print("利率值:", rtval)
print("总还款额:", total)
print("每月还款额:", monthlypayment)

result:

输入您的姓名mm
输入您的贷款金额1,000,000
输入您的贷款利率25
输入贷款期限):5
**************************************************
客户姓名 mm
贷款金额 1000000.0 利率 0.25 期限 5.0利率值 1250000.0
总还款额 2250000.0
每月还款额 37500.0

进程已完成退出代码为0

感谢 @Tim Roberts 的帮助现在我已经缩短了这段代码

clloan = input("输入您的贷款金额:")

if clloan.__contains__(","):
    clloan = clloan.replace(",", "")

if clloan.__contains__(" "):
    clloan = clloan.replace(" ", "")

clloan = float(clloan)

clrate = input("输入您的贷款利率:")
if clrate.__contains__("%"):
    clrate = clrate.replace("%", "")

if clrate.__contains__(" "):
    clrate = clrate.replace(" ", "")

clrate = float(clrate)

if clrate > 1:
    clrate = clrate / 100

请注意,我已经翻译了您提供的代码片段。如果需要任何其他帮助,请告诉我。

英文:

thank you from your answers i get the idea

i used contains and replace to fix the input numbers and here is the last solution:

clname=input("Enter Your Name: ")
clloan=input("Enter your loan value: ")
clloan= float(clloan.replace('$','').replace(',','').replace(' ',''))
clrate=input("enter your loan rate : ")
clrate = float(clrate.replace('%','').replace(',','').replace(' ',''))
if clrate > 1:
clrate = clrate / 100
clyears=float(input("enter loan period in years: "))
rtval=clloan * clrate * clyears
total=clloan + rtval
monthlypayment=total / (clyears * 12)
print("**************************************************")
print("Client: ", clname)
print("a loan of: ",clloan , " ,at rate: ",clrate , " ,on period: " ,clyears," years")
print("rate value is: ", rtval)
print("total payment will be: ",total)
print("and your monthly payment will be: ",monthlypayment)

result:

Enter Your Name: mm
Enter your loan value: 1,000,000
enter your loan rate : 25
enter loan period in years: 5
**************************************************
Client:  mm
a loan of:  1000000.0  ,at rate:  0.25  ,on period:  5.0  years
rate value is:  1250000.0
total payment will be:  2250000.0
and your monthly payment will be:  37500.0
Process finished with exit code 0

Thanks to @Tim roberts , i now shortened this code:

clloan=input("Enter your loan value: ")
if clloan.__contains__(","):
clloan=clloan.replace(",", "")
if clloan.__contains__(","):
clloan = clloan.replace(" ", "")
clloan = float(clloan)
clrate=input("enter your loan rate : ")
if clrate.__contains__("%"):
clrate=clrate.replace("%", "")
if clrate.__contains__(" "):
clrate=clrate.replace(" ", "")
clrate = float(clrate)
if clrate > 1:
clrate = clrate / 100

huangapple
  • 本文由 发表于 2023年5月11日 01:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定