简体   繁体   中英

How would I assign this variable?

I barely started this python tutorial and I don't understand how I would set this variable(total)

在此处输入图片说明

Think about it this way: Your Variable meal now stores two meals plus the tax for both of them. The only thing that is missing is the tip.

Now you can create this

total = meal + tip

if meal or tip is changing, the result of total would change too. But you need to be careful to not get confused with this behaviour.

Variables are bound to their object content if you assign them directly like

variable = 1234

if you now got another variable that stores the same content you would (normally) not write this again:

vari2 = 1234

but instead:

vari2 = variable

The interesting about that is, if you print

print variable
print vari2

you get the same results as expected. But if you change your first variable after all this code, there will be a difference! You can try to run this last code piece to understand what i mean:

vari = 1234 #Integer variable
print "Vari: %r" % vari
varia = vari #The Variable varia is bound to the CONTENT of vari not to the Name vari!
print "varia from vari: %r\n... -> New vari " % (varia)
vari = 42 # Now if you change vari, the content of varia is still the same!
print "Varia %r from vari %r" % (varia, vari)

This behaviour is very useful to store the original starting value of a variable. So you can keep track how the variable changed over time. I suggest to play a little with variables and print to get a better understanding.

您已经在前几行中使用了相同的概念。

total = meal + (meal * tax)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM