愚蠢的Python初学者
我很难理解为什么高中时教给我的这个“快速数学”公式似乎并不正确 前提是拿你的小时工资,翻倍,加上三个零,如果你一年中全职工作50周,结果大致相当于你的年薪愚蠢的Python初学者,python,expression,Python,Expression,我很难理解为什么高中时教给我的这个“快速数学”公式似乎并不正确 前提是拿你的小时工资,翻倍,加上三个零,如果你一年中全职工作50周,结果大致相当于你的年薪 # Preface print '---> Want to know your yearly salary? <---'.upper() # Question money = raw_input("How much money do you earn per hour?") # Math Work mult = money *
# Preface
print '---> Want to know your yearly salary? <---'.upper()
# Question
money = raw_input("How much money do you earn per hour?")
# Math Work
mult = money * 2
result = mult + str(000)
# Answer
print "you make roughly $%r per year, Working full-time for 50 weeks out of the year" % result
序言
打印-->想知道你的年薪吗 你想用一个字符串来做数学题。首先将其转换为整数:
money = int(raw_input("How much money do you earn per hour?"))
和乘法,而不是试图在末尾添加字符串
result = money * 2000
不过,如果您真的愿意,可以将整数转换回字符串,在末尾添加3个零:
mult = money * 2
strmult = str(mult)
result = strmult + '000'
你想用一个字符串来做数学题。首先将其转换为整数:
money = int(raw_input("How much money do you earn per hour?"))
和乘法,而不是试图在末尾添加字符串
result = money * 2000
不过,如果您真的愿意,可以将整数转换回字符串,在末尾添加3个零:
mult = money * 2
strmult = str(mult)
result = strmult + '000'
你知道下面的答案会给你想要的答案,对吗
#Math Work
mult = money * 2000
你知道下面的答案会给你想要的答案,对吗
#Math Work
mult = money * 2000
你把所有的类型都弄错了
raw_input
获取一个字符串,因此money
也被获取。因此,当您执行mult=money*2
操作时,您不是将数字加倍,而是一个字符串;因此,编写money*2
可以创建一个字符串,该字符串是您提供的字符串的两个副本的串联。如果输入10
,mult
将是'1010'
另外,在str(000)
000
中,它是一个整数,所以它实际上是一个普通的0
<因此,code>str(000)会产生'0'
,它会连接到您的双精度字符串1010
与'0'
=>10100
相连
你真正想要的是
# Question
money = int(raw_input("How much money do you earn per hour?"))
# Math Work
mult = money * 2
result = str(mult) + "000"
顺便说一句,加上零之类的数字对人类来说是很好的,但是因为我们是在处理一台计算机,你只需要乘以2000:
result = 2000*int(raw_input("How much money do you earn per hour?"))
你把所有的类型都弄错了
raw_input
获取一个字符串,因此money
也被获取。因此,当您执行mult=money*2
操作时,您不是将数字加倍,而是一个字符串;因此,编写money*2
可以创建一个字符串,该字符串是您提供的字符串的两个副本的串联。如果输入10
,mult
将是'1010'
另外,在str(000)
000
中,它是一个整数,所以它实际上是一个普通的0
<因此,code>str(000)会产生'0'
,它会连接到您的双精度字符串1010
与'0'
=>10100
相连
你真正想要的是
# Question
money = int(raw_input("How much money do you earn per hour?"))
# Math Work
mult = money * 2
result = str(mult) + "000"
顺便说一句,加上零之类的数字对人类来说是很好的,但是因为我们是在处理一台计算机,你只需要乘以2000:
result = 2000*int(raw_input("How much money do you earn per hour?"))
首先,当您读取用户输入时,
money
是一个字符串。因此,当用户输入10
时,您会得到'10'
因此,当您执行
money*2
时,您不会得到预期的20
。相反,您得到的是'10'*2
,它是两次串联的'10'
,即/e/'1010'
接下来,000
是一个计算结果为0
的int
,其str
为'0'
。您想添加的是'000'
我会这样做:
# Preface
print '---> Want to know your yearly salary? <---'.upper()
# Question
money = int(raw_input("How much money do you earn per hour?"))
# Math Work
mult = money * 2
result = str(mult) + "000"
序言
打印-->想知道你的年薪吗?想知道你的年薪吗 首先,当您读取用户输入时,
money
是一个字符串。因此,当用户输入10
时,您会得到'10'
因此,当您执行
money*2
时,您不会得到预期的20
。相反,您得到的是'10'*2
,它是两次串联的'10'
,即/e/'1010'
接下来,000
是一个计算结果为0
的int
,其str
为'0'
。您想添加的是'000'
我会这样做:
# Preface
print '---> Want to know your yearly salary? <---'.upper()
# Question
money = int(raw_input("How much money do you earn per hour?"))
# Math Work
mult = money * 2
result = str(mult) + "000"
序言
打印-->想知道你的年薪吗?想知道你的年薪吗<代码>#前言
打印-->想知道你的年薪吗<代码>#前言
打印-->想知道你的年薪吗 raw\u input()
函数返回一个字符串。
当您将money
乘以一个数字时,不是将整数值相乘,而是将字符串值相乘。这会导致变量的新值是字符串的倍数,或字符串重复多次。我建议使用money上的money=int(money)
函数将其转换为整数,或者更好地使用money=float(money)
来获取浮点数。raw_input()
函数返回一个字符串。
当您将money
乘以一个数字时,不是将整数值相乘,而是将字符串值相乘。这会导致变量的新值是字符串的倍数,或字符串重复多次。我建议使用money上的money=int(money)
函数将其转换为整数,或者更好的方法是money=float(money)
获取浮点数。试试这个
money=int(input('how much you make an hour'))
final_yearly=money*2000
print(final_yearly)
试试这个
money=int(input('how much you make an hour'))
final_yearly=money*2000
print(final_yearly)
您忘了提到原始输入(…)
获取字符串。您的代码将产生一个以'1010101010101010101010'
开头的2000个字符的字符串。您忘了提到原始输入(…)
获取一个字符串。您的代码将产生一个以'1010101010101010101010'