首先尝试使用Python! [英] First try at Python!
问题描述
各位大家好,我刚开始用Python编写代码。我之前没有任何其他编程语言的经验,所以我希望这个论坛对我来说是一个学习编码的好地方!所以这里是:
import random
bankroll = 10
bankroll2 = 10
< span class =code-keyword> def player():
player1 = random.randrange( 0 , 5 )
player2 = random.randrange( 0 , 5 )
if player1> player2:
print 播放器1抛出一个:,player1
print 玩家2抛出:,玩家2
打印 玩家1赢
bankroll + 1
bankroll2 - 1
打印 bankroll
print bankroll2
elif player1< player2:
print 播放器1抛出一个:,player1
print 玩家2抛出:,玩家2
打印 玩家2赢
打印 bankroll
打印 bankroll2
其他:
print 播放器1抛出:,player1
print 播放器2抛出:,player2
print 这是一个平局!
print bankroll
print bankroll2
print
player()
player()
player()
player()
player()
player()
player()
现在资金总是重新启动到10,我理解为什么,但我不知道如何避免它。我的目标是跟踪资金。
如果有人能指出我正确的方向,我将不胜感激。
以下语句只计算值
bankrol1 + 1
bankroll2 - 1
但这就是全部,他们不保存它们。它们应该是
bankroll + = 1 # 将1添加到bankrol1
bankroll2 - = 1 # 从bankroll2减去1
您还需要为案例添加两个语句当player2获胜时。我想如下:
bankrol1 - = 1 # 从bankrol1减去1
bankroll2 + = 1 # 将1添加到bankroll2
扩展到之前的回答,这归结为可变性。
基本上,整数是不可变的。添加两个整数时,它返回一个全新的整数。在这种情况下,您有一个变量bankroll,它被指定为原始整数。向此变量添加1时,该操作将返回一个新整数。你需要用更新的变量覆盖原始变量。
# 为变量指定一个整数
bankroll = 10
# 我们会将其分开,以便于查看。
# 基本上是bankroll + 1返回加法运算的结果
# 我们现在将它存储到临时变量
temp = bankroll + 1
# 使用 $ b的值覆盖原始bankroll变量$ b # 临时变量
bankroll = temp
现在我们合作请按以下方式在一行中执行此操作
bankroll = bankroll + 1
如果不使用临时变量,这会做同样的事情。但是,python有一些很好的语法糖来进行这种操作
bankroll + = 1
实际上这可用于许多基本操作
bankroll = 10
print (bankroll)# 打印10
bankroll + = 1
print (bankroll)# 打印11
bankroll - = 1
print (bankroll)# 打印10
bankroll * = 2
print (bankroll)# 打印20
bankroll / = 2
打印(bankroll)# 打印10
Hello everyone, i just started coding in Python. I have no prior experience in any other programming language so i hope this forum will be a great place for me to learn about coding! so here it is:
import random
bankroll = 10
bankroll2 = 10
def player():
player1 = random.randrange(0, 5)
player2 = random.randrange(0, 5)
if player1 > player2:
print "Player 1 throws a :", player1
print "Player 2 throws a :", player2
print "Player 1 Win"
bankroll + 1
bankroll2 -1
print bankroll
print bankroll2
elif player1 < player2:
print "Player 1 throws a :", player1
print "Player 2 throws a :", player2
print "Player 2 Win"
print bankroll
print bankroll2
else:
print "Player 1 throws a :", player1
print "Player 2 throws a :", player2
print "It's a tie!"
print bankroll
print bankroll2
print ""
player()
player()
player()
player()
player()
player()
player()
right now the bankrolls are always restarting to 10, i understand why but i don't know how to avoid it. My goal is to keep track of the bankroll.
If anyone could point me in the right direction it would be greatly appreciated.
The following statements just calculate the values
bankrol1 + 1 bankroll2 -1
but that is all, they do not save them. They should be
bankroll += 1 # add 1 to bankrol1 bankroll2 -= 1 # subtract 1 from bankroll2
You also need to add two statements for the case when player2 wins. I would guess something like:
bankrol1 -= 1 # subtract 1 from bankrol1 bankroll2 += 1 # add 1 to bankroll2
To extend on the previous answer, this comes down to mutability.
Basically, integers are immutable. When you add two integer it returns a brand new integer. In this case, you have a variable bankroll which is assigned to be the original integer. When you add 1 to this variable, the operation returns a new integer. You need to overwrite the original variable with the updated one.
# Assign an integer to your variable bankroll = 10 # We'll separate this out for ease of viewing. # basically bankroll + 1 returns the result of the addition operation # We'll store it to a temp variable for now temp = bankroll + 1 # Overwrite the original bankroll variable with the value of the # temp variable bankroll = temp
Now we could do this in one line as follows
bankroll = bankroll + 1
This does the same thing without using a temporary variable. However, python has some nice syntactic sugar for doing this sort of operation
bankroll += 1
In fact this are available for many of the basic operations
bankroll = 10 print(bankroll) # prints 10 bankroll += 1 print(bankroll) # prints 11 bankroll -= 1 print(bankroll) # prints 10 bankroll *= 2 print(bankroll) # prints 20 bankroll /= 2 print(bankroll) # prints 10
这篇关于首先尝试使用Python!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!