在我的程序中,我需要进行简单的数学计算,但是我的变量在str
处定义,因此我需要对int
进行计算以求和和。
例如:
当age = 40时,我得到404040404040(是40的6倍) 被读取为“ str”之类的res,我需要“ int”。
def check_age(age):
age = int(age)
return 30 * 6 if age >= 30 else 0
def just_married():
sum_married = 0
woman_age = int(input("Please enter the wife age [we know that is not ok ask woman about she's age] : ").strip())
sum_married = sum_married + check_age(woman_age)
husband = int(input("Please enter the husband age : ").strip())
sum_married = sum_married + check_age(husband)
return int(sum_married)
def children_age(number_of_children):
sum_children = number_of_children*50
return int(sum_children)
def work_hard():
print("for WIFE - Are you working part-time (0.5) or full-time (1)? : ")
wife_work = input()
print("for HUSBAND = Are you working part-time (0.5) or full-time (1)? : ")
husband_work = input()
sum_work = (wife_work+husband_work)*75
return int(sum_work)
def main():
sum_func = 0
print("The following is a program aimed at examining eligibility conditions "
"for participation in the tender Housing for the occupant.")
sum_func += just_married()
print("How many children over the age of 18 do you have? : ")
children = input()
sum_func += children_age(children)
sum_func += work_hard()
program_number = 599
if sum_func > program_number:
print("you got : " + str(sum_func) + " points ")
else:
print("sorry, but you need " + str(program_number-sum_func) + " point to join the program. try next time.")
main()
编辑: 我编辑代码,并在功能“ check_age”处进行了新更改,然后更新了完整代码。
这是输入:
The following is a program aimed at examining eligibility conditions for participation in the tender Housing for the occupant.
Please enter the wife age [we know that is not ok ask woman about she's age] : 40
Please enter the husband age : 50
How many children over the age of 18 do you have? :
4
for WIFE - Are you working part-time (0.5) or full-time (1)? :
1
for HUSBAND = Are you working part-time (0.5) or full-time (1)? :
1
you got : 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111155555555555555555555555555555555555555555555555915 points
Process finished with exit code 0
答案 0 :(得分:1)
在您的函数check_age
中:
def check_age(age):
age = int(age)
return 30 * 6 if age >= 30 else 0
另外,请考虑更改以下行:
print("Please enter the wife age [we know that is not ok ask woman about she's age] : ")
woman_age = input(int)
对此:
woman_age = int(input("Please enter the wife age [we know that is not ok ask woman about she's age] : ").strip())
说明:
input
接收字符串,将其打印并等待用户输入。str.strip
删除尾随空格int
将变量转换为整数清理输入内容后,可以在check_age
中删除对int的显式转换:
def check_age(age):
return 30 * 6 if age >= 30 else 0
[编辑]还有一些建议:
sanitize_input
,要求输入
广告返回一个整数。然后在任何地方使用它来替换{{1}} ... print
input
之类的函数中处理变量,或者在所有函数中进行处理。如果这样做,对于您和对您有帮助的人,您的代码将更容易理解,并且更不易出错答案 1 :(得分:0)
在尝试进行任何计算之前,必须先完成字符串->数字转换,在module.exports.shipments = async (event) => {
const axios = require("axios");
let data = JSON.parse(event.body);
let url = data.apiURL + "/api/1.1/wf/bulkshipments";
let patchURL = data.apiURL + "/api/1.1/obj/company/" + data.companyID;
let good = [];
let bad = [];
let promises = data.shipments.map(item =>
axios.post(url, {
batchID: data.batchID,
companyID: data.companyID,
shipment: item
})
//.then( res => good.push({"Order Reference": item.order_reference, "result": res.data}))
//.catch( error => bad.push({"Order Reference": item.order_reference, "result": error.response.data}))
);
await Promise.all(promises);
return {
statusCode: 200,
body: JSON.stringify({
message: 'success',
totalShipments: data.shipments.length,
created: good.length,
errors: bad.length,
result: {"completed": good, "failed": bad}
}, null, 2),
};
};
时为时已晚,因为已经发生了奇怪的事情。记住/了解到Python可以添加字符串(许多语言都通用,return
在其他各种语言中也都是"1"+"1"
),此外,它还可以将它们与整数{ {1}}是"11"
。
如果您假设用户输入正确,那么最简单的方法就是在"1"*5
行中进行转换,这已经发生在"11111"
中,但在其他地方则没有。
当然,请记住,当您希望输入input
之类的内容时,应将其转换为just_married
。
0.5