Jump to content
name = str(input("Hello, what is your name?"))

print ("Hello "+name)

question = str(input("Please press: 1 for multiplication, 2 for addition, 3 for division, or 4 for subtraction."))

#<---singular loop--->

mult = 1 

if mult:

    print ("Ok let's do some multiplication.")

    num1 = int(input("Number 1: "))

    num2 = int(input("Number 2: "))

    print ("So your numbers are: ",(num1),(num2)) 

    print ("Here is your answer:")

    print (num1 * num2)    

else:

    print ("Sorry please try one of the listed numbers.")

add = 2

if add:

    print ("Ok let's do some addition.")

    num3 = int(input("Number 1: "))

    num4 = int(input("Number 4: "))

    print ("So your numbers are: ", (num3), (num4))

    print ("Here is your answer:")

    print (num3 + num4)

else:

    print ("Sorry please try one of the listed numbers.") 

 

 

Ok so i have recently began using python and am trying to allow the user to enter in a number(either 1,2,3, or 4) and then based off of what number was entered have the corresponding math type to open. I think the code itself will probably explain what i am trying to do. Anyway my issue is when i get to the part when it asks for a number and i enter 1 for multiplication, multiplication opens like it is supposed to, but if i enter in 2 for addition multiplication still opens. Infact no mater what i enter the multiplication opens. Ugh i cant figure this out someone please help!

I run my own indie game company called Color Dragon Studios where we are currently making a 2d platformer game called Small Earth.

Link to comment
https://linustechtips.com/topic/326480-pyhon-begginer-help/
Share on other sites

Link to post
Share on other sites

what you are doing by saying "mult=1" is setting mult to True

this means when you do "if mult" you are basically doing "if True" and since its always true it will always run

 

if you want to check for an input value you can just do "if question==1" and "if question==2"

 

here is a version of your code that should work:

name = str(input("Hello, what is your name?"))print ("Hello "+name)question = int(input("Please press: 1 for multiplication, 2 for addition, 3 for division, or 4 for subtraction."))#<---singular loop---> if question==1:    print ("Ok let's do some multiplication.")    num1 = int(input("Number 1: "))    num2 = int(input("Number 2: "))    print ("So your numbers are: ",(num1),(num2))     print ("Here is your answer:")    print (num1 * num2)     elif question==2:    print ("Ok let's do some addition.")    num3 = int(input("Number 1: "))    num4 = int(input("Number 4: "))    print ("So your numbers are: ", (num3), (num4))    print ("Here is your answer:")    print (num3 + num4)else:    print ("Sorry please try one of the listed numbers.") 

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/326480-pyhon-begginer-help/#findComment-4434657
Share on other sites

Link to post
Share on other sites

I'm not familiar with Python, but your condition is checking the variable mult (which is 1, so true), not the input, so it's going to always execute the mult if block. Also, edit your post using the code formatting (the blue <> symbol) like this:

print("Hello World")

CPU: i7-4790K --- HEATSINK: NZXT Kraken X61 --- MOBO: Asus Z97-A --- GPU: GTX 970 Strix --- RAM: 16GB ADATA XPG --- SSD: 512GB MX100 | 256GB BX200 HDD: 1TB WD Black --- PSU: EVGA SuperNova G2 --- CASE: NZXT H440 --- DISPLAY3 x Dell U2414H --- KEYBOARD: Pok3r (Clears) --- MOUSE: Logitech G Pro --- OS: Windows 10

Link to comment
https://linustechtips.com/topic/326480-pyhon-begginer-help/#findComment-4434665
Share on other sites

Link to post
Share on other sites

 

what you are doing by saying "mult=1" is setting mult to True

this means when you do "if mult" you are basically doing "if True" and since its always true it will always run

 

if you want to check for an input value you can just do "if question==1" and "if question==2"

 

here is a version of your code that should work:

name = str(input("Hello, what is your name?"))print ("Hello "+name)question = int(input("Please press: 1 for multiplication, 2 for addition, 3 for division, or 4 for subtraction."))#<---singular loop---> if question==1:    print ("Ok let's do some multiplication.")    num1 = int(input("Number 1: "))    num2 = int(input("Number 2: "))    print ("So your numbers are: ",(num1),(num2))     print ("Here is your answer:")    print (num1 * num2)     elif question==2:    print ("Ok let's do some addition.")    num3 = int(input("Number 1: "))    num4 = int(input("Number 4: "))    print ("So your numbers are: ", (num3), (num4))    print ("Here is your answer:")    print (num3 + num4)else:    print ("Sorry please try one of the listed numbers.") 

I actually ended up figureing it out while waiting for a response, although my is different. Thank you very much for your answer, i love being able to ask questions on here of any sort and get a reliable answer. Anyway this is what i did:

 

 

name = str(input("Hello, what is your name?"))
print ("Hello "+name)
question = str(input("Please press: 1 for multiplication, 2 for addition, 3 for division, or 4 for subtraction. Please press enter and be ready to make your decision."))
#<---singular loop--->
usr_input = input("Input: ")
while (usr_input != '1') and (usr_input != '2'):
    usr_input = input("Input: ")
if usr_input == '1':
    print ("Ok let's do some multiplication!")
    num1 = int(input("Number 1: "))
    num2 = int(input("Number 2: "))
    print ("You entered: "),(num1),(num2)
    print (num1 * num2)
else:
    print ("Please enter a valid number.")
if usr_input == '2':
    print ("Ok let's do some addition.")
    num3 = int(input("Number 1: "))
    num4 = int(input("Number 4: "))
    print ("So your numbers are: ", (num3), (num4))
    print ("Here is your answer:")
    print (num3 + num4)
else:
    print ("Sorry please try one of the listed numbers.") 

I run my own indie game company called Color Dragon Studios where we are currently making a 2d platformer game called Small Earth.

Link to comment
https://linustechtips.com/topic/326480-pyhon-begginer-help/#findComment-4434721
Share on other sites

Link to post
Share on other sites

 

I actually ended up figureing it out while waiting for a response, although my is different. Thank you very much for your answer, i love being able to ask questions on here of any sort and get a reliable answer. Anyway this is what i did:

yup that works too :)

and you only need one "else", it should be at the end of all the if statements

 

you are currently using two "if"s but you should be doing "if" then "elif" and finishing with "else" ;)

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/326480-pyhon-begginer-help/#findComment-4434752
Share on other sites

Link to post
Share on other sites

 

I actually ended up figureing it out while waiting for a response, although my is different. Thank you very much for your answer, i love being able to ask questions on here of any sort and get a reliable answer. Anyway this is what i did:

 

 

name = str(input("Hello, what is your name?"))
print ("Hello "+name)
question = str(input("Please press: 1 for multiplication, 2 for addition, 3 for division, or 4 for subtraction. Please press enter and be ready to make your decision."))
#<---singular loop--->
usr_input = input("Input: ")
while (usr_input != '1') and (usr_input != '2'):
    usr_input = input("Input: ")
if usr_input == '1':
    print ("Ok let's do some multiplication!")
    num1 = int(input("Number 1: "))
    num2 = int(input("Number 2: "))
    print ("You entered: "),(num1),(num2)
    print (num1 * num2)
else:
    print ("Please enter a valid number.")
if usr_input == '2':
    print ("Ok let's do some addition.")
    num3 = int(input("Number 1: "))
    num4 = int(input("Number 4: "))
    print ("So your numbers are: ", (num3), (num4))
    print ("Here is your answer:")
    print (num3 + num4)
else:
    print ("Sorry please try one of the listed numbers.") 

 

 

Your code is a bit redundant, since you have two if's, each with an essentially identical else clause. Go from

if usr_input == '1':else:if usr_input == '2':else:

to the following:

if usr_input == '1':elif usr_input == '2':else:

Also, you ask for an input with the name question, but never use it. And since I'm on that, there's no need to turn an input into string, use raw_input instead, which you should use anyway instead of input.

I own and use, sorted from newest to oldest: SteelSeries 6Gv2. Microsoft SideWinder X4. Mionix Naos 7000. Zowie EC1 Evo. Microsoft SideWinder X8. Microsoft IntelliMouse Explorer 3.0. Dell U2414H. Samsung P2270H. AKG K273 Pro. Sennheiser HD555. Razer Goliathus Speed Medium. Func 1030 L. Qpad CT Medium.

I used to own: Razer DeathAdder 3G. Razer Krait. IntelliMouse Optical 1.1. SteelSeries QcK.

Link to comment
https://linustechtips.com/topic/326480-pyhon-begginer-help/#findComment-4434782
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

×