如何将字符串转换为整数?

时间:2016-09-14 14:11:15

标签: python python-3.x

我正在为我的编程类做一些代码,我们需要制作一个程序来计算构建桌面的成本。我需要帮助将DrawerAmount更改为整数,而不是字符串!

def Drawers():
print("How many drawers are there?")
DrawerAmount = input(int)
print("Okay, I accept that the total amount of drawers is " + DrawerAmount + ".")
return DrawerAmount

def Desk():
    print("What type of wood is your desk?")
    DeskType = input()
    print("Alright, your desk is made of " + DeskType + ".")
    return DeskType

def Calculation(DrawerAmount, DeskType):
    if "m" in DeskType:
        FinalPrice = DrawerAmount * 30 + 180
    elif "o" in DeskType:
        FinalPrice = DrawerAmount * 30 + 140
    elif "p" in DeskType:
        FinalPrice = DrawerAmount * 30 + 100

def Total():
    print("The final price is " + FinalPrice )

DrawerAmount = Drawers()
DeskType = Desk()
Calculation(DrawerAmount, DeskType)
FinalPrice = Total()

2 个答案:

答案 0 :(得分:0)

  

我需要帮助将DrawerAmount更改为整数,而不是字符串!

试试这个:

v = int(DrawerAmount) 

答案 1 :(得分:0)

def Drawers():
    draweramount = input("How many drawers are there?")
    print("Okay, I accept that the total amount of drawers is " + draweramount +     ".")
    return int(draweramount)

def Desk():
    desktype = input("What type of wood is your desk?")
    print("Alright, your desk is made of " + desktype + ".")
    return desktype

def Calculation(draweramount, desktype):
    if "m" in desktype:
        finalprice = draweramount * 30 + 180
    elif "o" in desktype:
        finalprice = draweramount * 30 + 140
    elif "p" in desktype:
        finalprice = draweramount * 30 + 100
    return finalprice

draweramount = Drawers()
desktype = Desk()
finalprice=Calculation(draweramount, desktype)
print("The final price is ",Calculation(draweramount,desktype) )
  

如果输入不是" P"您还没有指定输出。或" M"或者" O"。   将您的函数用小写字母包含 _ ,只要它是两个或多个单词的组合   例如。 function_name()