你如何在python中添加变量的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14500742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:38:52  来源:igfitidea点击:

how do you add the values of variables in python?

python

提问by user2007306

I am trying to add the values of variables on python but it doesn't seem to work.

我正在尝试在 python 上添加变量的值,但它似乎不起作用。

I want to try and add the values of the variables together even if the value is either True or False: please - can anyone help?

即使值是 True 或 False,我也想尝试将变量的值加在一起:拜托 - 任何人都可以帮忙吗?

final=0
print ("welcome to my binary converter")
print("first number is the smallest value")
print ("please enter a 0 or a 1 five times")

in1 = input("number 1 please")

if in1 == 1:
    final=final+1
elif in1 == 0:
    final=final+0
elif ValueError
    print("please enter a 1 or a 0")

in2 = input("number 2 please")

if in2 == 1:
    final=final+2
elif in2 == 0:
    final=final+0
elif ValueError
    print("please enter a 1 or a 0")

in3 = input("number 3 please")

if in3 == 1:
    final=final+4
elif in3 == 0:
    final=final+0
elif ValueError
    print("please enter a 1 or a 0")

in4 = input("number 4 please")

if in4 == 1:
    final=final+8
elif in4 == 0:
    final=final+0
elif ValueError
    print("please enter a 1 or a 0")

in5 = input("number 5 please")

if in5 == 1:
    final=final+16
elif in5 == 0:
    final=final+0
elif ValueError
    print("please enter a 1 or a 0")
print(final)

And I have tried looking on this website, python books, help online, python teacher, etc.

我试过在这个网站上查找,python 书籍,在线帮助,python 老师等。

Also, I have version 3.3.0 if that helps.

另外,如果有帮助,我有 3.3.0 版。

Jacob

雅各布

welcome to my binary converter

欢迎使用我的二进制转换器

first number is the smallest value

第一个数字是最小值

please enter a 0 or a 1 five times

请输入 0 或 1 五次

number 1 please1

请 1 号 1

please enter a 1 or a 0

请输入 1 或 0

number 2 please0

2号请0

please enter a 1 or a 0

请输入 1 或 0

number 3 please0

3号请0

please enter a 1 or a 0

请输入 1 或 0

number 4 please1

4号请1

please enter a 1 or a 0

请输入 1 或 0

number 5 please1

请 5 号 1

please enter a 1 or a 0

请输入 1 或 0

0

0

it seems to not change the value of final

它似乎没有改变 final 的值

采纳答案by TerryA

You can only add variables together if both variables are either an integer or a string, but not a boolean (well, you kinda can but it's not effective). For example:

如果两个变量都是整数或字符串,而不是布尔值,则只能将变量添加在一起(好吧,您可以,但它无效)。例如:

>>> var = 1
>>> var2 = 4
>>> var + var2
5

>>> stringvar = 'Hello '
>>> stringvar2 = 'world.'
>>> stringvar + stringvar2
'Hello world.'

>>> boolean1 = True
>>> boolean2 = False
>>> boolean1 + boolean2
1

The reason that works is because:

有效的原因是:

>>> True == 1
True
>>> False == 0
True


EDIT:

编辑:

It seems you have added in more code, so I'll show you what you're doing wrong.

看来您添加了更多代码,所以我会告诉您您做错了什么。

The reason you're getting a Syntax Error is because you have elif ValueError. This doesn't work. First, there isn't even a ValueError, there can't be as you have an input(). If you want to check whether a number is not 0 or 1, do this:

您收到语法错误的原因是因为您有elif ValueError. 这不起作用。首先,甚至没有 ValueError,因为你有一个 input(),所以不可能。如果要检查数字是不是 0 或 1,请执行以下操作:

in1 = input("number 1 please")

if in1 == 1:
    final += 1 # I've also changed this. final += 1 is the same as final = final + 1
elif in1 == 0:
    final += 0
elif in1 != 1 or in1 != 0:
    print("please enter a 1 or a 0")

I highly suggest reading over some python tutorials. This is some basic syntax.

我强烈建议阅读一些 Python 教程。这是一些基本的语法。