税务计算器只打印原始输入,不打印

2024-05-14 18:52:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个非常简单的税务计算器来测试我对Python的学习,它只打印原始输入而不是结果

我试着搞乱变量-我得到了错误,并意识到这是因为我在函数外定义了原始变量,但不确定还要尝试什么

import math

def tax(s):
  s = input("What is the bill?")
  tax_added = s * .07
  total = s + tax_added
  print(total)

我期望加税的总数,但我只得到S的结果


Tags: 函数importaddedinput定义isdef错误
2条回答

input函数提供字符串:

 s = input("What is the bill?")

如果你想让它成为一个数字,请将其转换为:

s = float(input("What is the bill?"))

似乎您没有将输入的结果(字符串)转换为float(我假设您希望您的输入是float)

import math

def tax(s):
  s = float(input("What is the bill?"))
  tax_added = s * .07
  total = s + tax_added
  print(total)

一个观察,如果你要用你的输入替换它,为什么要把s作为参数?我是说,你可以摆脱它

根据我所说的:

def tax():
    s = float(input("What is the bill?"))
    tax_added = s * .07
    total = s + tax_added
    print(total)

示例

>>> tax()
What is the bill?10
10.7

相关问题 更多 >

    热门问题