Jump to content

Hello, I just started Python programming and I have started experimenting with all the math operators, and I am trying to make the program add three user inputs, two of which are changed through operations into a new variable, and I want to add the two new variables with the last one. The error I am getting is the following:

Traceback (most recent call last):
  File "D:\PythonCode\function.py", line 6, in <module>
    print(a1+b1+c)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

Here is the code

a = int(input("Input your a variable ") )
a1 = print(int(a ** (2))) 
b = int(input("Input your b variable ") )
b1 = print(int(b * a))
c = int(input("Input your constant ") )
print(a1+b1+c)

I apologize for the horrendous explanation, but it was the best I could do.

Thanks

Link to comment
https://linustechtips.com/topic/749854-python-input-addition/
Share on other sites

Link to post
Share on other sites

a1 and b1 aren't ints. 

 

Instead do something like this: 


a=input("input your a variable")

a1 = a1*2

print a1

 

b=input("input your b variable")

b1=b*a

print b1

 

c=input("input your constant")

print a1+b1+c

 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/749854-python-input-addition/#findComment-9492531
Share on other sites

Link to post
Share on other sites

1 minute ago, Anglogang said:

My plan was to add the new variables a1 and b1 to c

see my edit

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/749854-python-input-addition/#findComment-9492547
Share on other sites

Link to post
Share on other sites

1 hour ago, Anglogang said:

Hello, I just started Python programming and I have started experimenting with all the math operators, and I am trying to make the program add three user inputs, two of which are changed through operations into a new variable, and I want to add the two new variables with the last one. The error I am getting is the following:

// The following will not do any type checking. If the user enters a character then it will break

user_one = int(input("Please enter an integer: "))
user_one = user_one ** 2
print(user_one)
  
user_two = int(input("Please enter another integer: "))
user_two *= user_one
print(user_two)
  
user_three = int(input("Please enter a third integer: "))
user_three += user_two + user_one
print(user_three)
  
  
// The following will do simple type checking. If the user does not enter an integer, then the program will respond appropriately.
  
def get_input():
	
	while True:
		try:
			user_value = int(input("Please enter an integer: "))
			return user_value
              
		except:
			print("You did not enter an integer. Please only enter an integer")
              

def main():
	user_a = get_input() ** 2
	print(user_a)
      
	user_b = get_input() * user_a
	print(user_b)
      
	user_c = get_input() + user_b + user_a
	print(user_c)

main()

 

ENCRYPTION IS NOT A CRIME

Link to comment
https://linustechtips.com/topic/749854-python-input-addition/#findComment-9492974
Share on other sites

Link to post
Share on other sites

15 minutes ago, straight_stewie said:

// The following will not do any type checking. If the user enters a character then it will break

user_one = int(input("Please enter an integer: "))
user_one = user_one ** 2
print(user_one)
  
user_two = int(input("Please enter another integer: "))
user_two *= user_one
print(user_two)
  
user_three = int(input("Please enter a third integer: "))
user_three += user_two + user_one
print(user_three)
  
  
// The following will do simple type checking. If the user does not enter an integer, then the program will respond appropriately.
  
def get_input():
	
	while True:
		try:
			user_value = int(input("Please enter an integer: "))
			return user_value
              
		except:
			print("You did not enter an integer. Please only enter an integer")
              

def main():
	user_a = get_input() ** 2
	print(user_a)
      
	user_b = get_input() * user_a
	print(user_b)
      
	user_c = get_input() + user_b + user_a
	print(user_c)

main()

 

agreed it is always better to update the first variable rather than set a new one, uses up more memory otherwise. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/749854-python-input-addition/#findComment-9493055
Share on other sites

Link to post
Share on other sites

1 hour ago, vorticalbox said:

agreed it is always better to update the first variable rather than set a new one, uses up more memory otherwise. 

On a side note, how would I get the input to accept multiple variable types? For example, allowing the input to except integers, fractions, and floating point numbers

Link to comment
https://linustechtips.com/topic/749854-python-input-addition/#findComment-9493454
Share on other sites

Link to post
Share on other sites

6 minutes ago, Anglogang said:

On a side note, how would I get the input to accept multiple variable types? For example, allowing the input to except integers, fractions, and floating point numbers

You can use the Fraction type and either leave it as a fraction or check the denominator for a 1 to convert to int or convert to float for anything else.

1474412270.2748842

Link to comment
https://linustechtips.com/topic/749854-python-input-addition/#findComment-9493499
Share on other sites

Link to post
Share on other sites

2 hours ago, Anglogang said:

On a side note, how would I get the input to accept multiple variable types? For example, allowing the input to except integers, fractions, and floating point numbers

The best way to make a calculator (which I believe is what you're really after) is to use Dijsktra's Shunting Yard Algorithm to parse infix expressions into post fix expressions and then execute the post fix expressions.

 

Accepting all of the possible types isn't really hard, it's just time consuming. There's multiple ways to do it, but the easiest to understand and implement would be to build nested try/except structures like I showed you in my previous example.

ENCRYPTION IS NOT A CRIME

Link to comment
https://linustechtips.com/topic/749854-python-input-addition/#findComment-9494449
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×