3

I'm trying to set up a situation in which the user inputs a number and an operator and the output is the (user number) (user operator) on a list of 1 through 10.

This is hard to explain, but here's the code:

num = int(input("Enter a number greater than 1: "))

oper = input("Choose a math operation (+, -, *): ")
for i in range(1, 11):
    print(num)

And I get lost there. I want to get something that looks like

num   oper   1   =   (whatever num and the operator and 1 equal)
num   oper   2   =   (whatever num and the operator and 2 equal)

And so on.

So my question is: How do you assign a user-inputted operator to a variable?

5 Answers 5

4

Another possibility is to use the operator module to set up a dictionary of operator functions, like so:

import operator

operator_dict = {
    '+': operator.add,
    '-': operator.sub,
    '*': operator.mul,
}
num = int(input("Enter a number greater than 1: "))

oper = input("Choose a math operation (+, -, *): ")
for i in range(1, 11):
    print(operator_dict[oper](float(num), i))

An example session:

Enter a number greater than 1: 3
Choose a math operation (+, -, *): *
3.0
6.0
9.0
12.0
15.0
18.0
21.0
24.0
27.0
30.0
Sign up to request clarification or add additional context in comments.

Comments

3

From what you say here:

I'm trying to set up a situation in which the user inputs a number and an operator and the output is the (user number) (user operator) on a list of 1 through 10.

I think you want to do this:

num = int(input("Enter a number greater than 1: "))

oper = input("Choose a math operation (+, -, *): ")
for i in range(1, 11):
    if oper == "+":
       print(num+i)
    elif oper == "-":
       print (num-i)
    elif oper == "*":
       print (num*i)

4 Comments

this is good but you could move the if block outside the loop
This is something that eval can be used for in a more efficient way.
Eval is something you generally want to avoid, even though it can help.
I prefer the if block (or a dictionary lookup) as it allows the programmer to control exactly which operations are allowed
3

Seems like you want something like this:

num = int(input("Enter a number greater than 1: "))

oper = raw_input("Choose a math operation (+, -, *): ")
for i in range(1, 11):
    if oper == '+':
        print(num, oper, i, '=', num + i)
    elif oper == '-':
        print(num, oper, i, '=', num - i)
    elif oper == '*':
        print(num, oper, i, '=', num * i)
    else:
        print('operator is not supported')

Output:

Enter a number greater than 1: 2
Choose a math operation (+, -, *): *
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20

Comments

2

Eval can be used here:

num = str(input("Enter a number greater than 1: "))

oper = str(input("Choose a math operation (+, -, *, /, %, //): "))
if oper in ["+", "-", "*", "/", "%", "//"]:
    for i in range(1, 11):
        operation = num + oper + str(i) #Combine the string that is the operation
        print("{} {} {} = {}".format(num,oper,str(i),eval(operation)))
else: #if it is not in our approved items
    print("Operation not supported.")

Comments

1

I think the easiest way to do this to create a function, which within itself has all these if/elif conditions, since python does not recognize strings as operators.

The function may look a little bit like:

def operation(number1, number2, operator):
    if operator == '+':
        return number1 + number2
    elif operator == '-':
        return number1 - number2

And so on. Then you can call this function in a for loop, like this:

for n in range(10):
    otherNumber = n + 1
    print(yourNumber, otherNumber, operator)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.