将整数加在一起并得到错误的答案
Adding int's together and getting the wrong anser
我正在编写一个赌场骰子游戏,要求用户输入赌注和骰子号码。骰子数的结果将从余额中扣除赌注或将赌注 * 6 添加到余额中。当用户猜错时,从余额中扣除正确的值。
余额为 490,下注 5 后赢了,新余额为 556045。这显然应该是 520。
为什么新余额会是 556045?
我的代码连同输出在下面
import random
print("Welcome to the casino")
UserName = input("Please enter your name: ")
print("Thanks for playing " + UserName + "! We wish you the best of luck!")
balanceRemaining = 500
while balanceRemaining != 0:
RandomNumber = random.randint(1, 6)
Wager = input("Please enter a wager: ")
UserNumber = input("Enter a number: ")
print("Dice number was " + str(RandomNumber))
if int(UserNumber) != RandomNumber:
balanceRemaining = balanceRemaining - int(Wager)
print("Your Balance is now: " + str(balanceRemaining))
else:
Winnings = int(Wager * 6)
balanceRemaining = int(Winnings) + int(balanceRemaining)
print("Your Balance is now: " + str(balanceRemaining))
输出
Please enter a wager: 5
Enter a number: 5
Dice number was 1
Your Balance is now: 495
Please enter a wager: 5
Enter a number: 5
Dice number was 6
Your Balance is now: 490
Please enter a wager: 5
Enter a number: 5
Dice number was 5
Your Balance is now: 556045
您需要先转换投注字符串然后乘以:
Winnings = int(Wager) * 6
Wager
是一个字符串,所以 '5'
并且允许乘以字符串,这会产生一个具有重复值的新字符串:
>>> '5' * 6
'555555'
然后转换该新字符串,产生比您预期的大得多的赢利。
最好尽早转换用户输入,这样就很难在代码的其他地方犯此类错误。这也有助于减少将输入转换为整数所需的位置数:
Wager = int(input("Please enter a wager: "))
UserNumber = int(input("Enter a number: "))
这真的很有趣。这里你输入一个字符串:
Wager = input("Please enter a wager: ")
而 Wager
在您的情况下是 字符串 "5"
。然后,您将 string 乘以 6:
Winnings = int(Wager * 6)
与Winnings = int("5"* 6)
相同,但是"5"* 6 == '555555'
!然后,int
将其转换为整数,您得到的结果不正确。
您想将什么转换为整数:字符串 "5"* 6
还是字符串 "5"
?你肯定是指:
Winnings = int(Wager) * 6
我正在编写一个赌场骰子游戏,要求用户输入赌注和骰子号码。骰子数的结果将从余额中扣除赌注或将赌注 * 6 添加到余额中。当用户猜错时,从余额中扣除正确的值。
余额为 490,下注 5 后赢了,新余额为 556045。这显然应该是 520。
为什么新余额会是 556045?
我的代码连同输出在下面
import random
print("Welcome to the casino")
UserName = input("Please enter your name: ")
print("Thanks for playing " + UserName + "! We wish you the best of luck!")
balanceRemaining = 500
while balanceRemaining != 0:
RandomNumber = random.randint(1, 6)
Wager = input("Please enter a wager: ")
UserNumber = input("Enter a number: ")
print("Dice number was " + str(RandomNumber))
if int(UserNumber) != RandomNumber:
balanceRemaining = balanceRemaining - int(Wager)
print("Your Balance is now: " + str(balanceRemaining))
else:
Winnings = int(Wager * 6)
balanceRemaining = int(Winnings) + int(balanceRemaining)
print("Your Balance is now: " + str(balanceRemaining))
输出
Please enter a wager: 5
Enter a number: 5
Dice number was 1
Your Balance is now: 495
Please enter a wager: 5
Enter a number: 5
Dice number was 6
Your Balance is now: 490
Please enter a wager: 5
Enter a number: 5
Dice number was 5
Your Balance is now: 556045
您需要先转换投注字符串然后乘以:
Winnings = int(Wager) * 6
Wager
是一个字符串,所以 '5'
并且允许乘以字符串,这会产生一个具有重复值的新字符串:
>>> '5' * 6
'555555'
然后转换该新字符串,产生比您预期的大得多的赢利。
最好尽早转换用户输入,这样就很难在代码的其他地方犯此类错误。这也有助于减少将输入转换为整数所需的位置数:
Wager = int(input("Please enter a wager: "))
UserNumber = int(input("Enter a number: "))
这真的很有趣。这里你输入一个字符串:
Wager = input("Please enter a wager: ")
而 Wager
在您的情况下是 字符串 "5"
。然后,您将 string 乘以 6:
Winnings = int(Wager * 6)
与Winnings = int("5"* 6)
相同,但是"5"* 6 == '555555'
!然后,int
将其转换为整数,您得到的结果不正确。
您想将什么转换为整数:字符串 "5"* 6
还是字符串 "5"
?你肯定是指:
Winnings = int(Wager) * 6