I'm working on a calculated but I have hit a brick wall. I'm seeing this error "TypeError: string indices must be integers" [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed yesterday.
Improve this question
def add (n1, n2):
return n1 + n2
def subtract(n1, n2):
return n1 - n2
def multiply(n1, n2):
return n1 * n2
def divide(n1, n2):
return n1 / n2
operations = {"+": "add",
"-": "subtract",
"*": "multiply",
"/": "divide"
}
num1 = int(input("What's your first number?: "))
num2 = int(input("What's your second number?: "))
for symbol in operations:
print(symbol)
operation_symbol = input("Pick an operation from line above: ")
calculation_function = operations[operation_symbol]
answer = calculation_function[num1, num2] # This is line where the string induces must be integers is happening
print(f"{num1} {operation_symbol} {num2} = {answer}")
I am expecting to see the out put of the calculation.

Related

max function not returning right ouput [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
(I am pretty new to python)
Hey guys, so I was trying to code a simple python program which finds the HCF of two numbers, but I am getting an error when the two numbers are co-prime and the actual HCF is not showing for the two numbers
(Ex: The HCF for 12 and 24 should be 12 but python is showing 4
Here is the code, can any of you guys help me?
factors = []
print("Enter first number")
num1 = int(input())
print("Enter second number")
num2 = int(input())
if num1 < num2:
lesser = num1
else:
lesser = num2
for i in range(2, lesser + 2):
if num1 % i == 0 and num2 % i == 0:
factors.append(i)
if not factors:
print(num1, "and", num2, "are co-prime numbers, so they don't have any common factors")
str_factors = ', '.join(map(str, factors))
hcf = max(str_factors)
print(hcf)
You are turning factors into a comma separated string and using max on the string. It's giving you the max of the string lexicographically. You need to use max on the factors variable.
Say for a string '2, 12, 42, 13' max will return '4' because it loops through all characters in a string
Fix this line and it should produce you expected output
hcf = max(factors)

I dont know whats wrong, SyntaxError: unexpected EOF while parsing [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
num1 = float(input())
num2 = float(input())
soma = (num1 * 3.5 + num2 * 7.5) / (2 * 10)
print(f'MEDIA = {soma.:5}')
...fails with:
RUNTIME ERROR
File "<fstring>", line 1
(soma.)
^
SyntaxError: unexpected EOF while parsing
Please visit this link for more info about string formatting in python
num1 = float(input())
num2 = float(input())
soma = (num1 * 3.5 + num2 * 7.5) / (2 * 10)
print(f'MEDIA = {soma:.5f}')
Output:
$ python3.10 main.py
3
4
MEDIA = 2.02500

i am trying to print out this simply equation, but it says float object is not callable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have been trying to print out this very simple equation but I cannot figure out why it will not print?
fc = float(input("first cycle is "))
fp = float(input("first percent is "))
sc = float(input("second cycle is "))
sp = float(input("second percent is "))
tc = float(input("third cycle is "))
tp = float(input("third percent is "))
mem = float(input("the memory is "))
first_part = (fc * fp + (sc(1-fp)))
print(first_part)
It gives me the error "TypeError: 'float' object is not callable"
The * multiplication operator cannot be omitted as it can in an algebraic equation. The interpreter thinks sc(1-fp) is a function call.
first_part = (fc * fp + (sc * (1-fp)))

It shows the error as 'fact' not defined.why? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
def factorial(n):
if(n == 1):
fact = 1
return fact
else:
fact = a * factorial(n - 1)
return fact
a = int(input("Enter a number to find its factorial: ")
if (a > 0):
print("The factorial of", a, "is:", fact)
else:
print("Enter a positive value to find its factorial")
In the above code it tells me that - NameError name 'fact' is not defined .
Lines starting from a = int... should be outside your function. Once that is done all you need is to add fact = factorial(a).
Find the correct logic below.
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = 7
# check if the number is negative
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))

how to take (6+5) as a single input from user in python and display the result [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
We are creating a calculator using python. I would like the user to input a number then operator then second number then the program prints out the result. For example, "User input calculation" ,User:5+9, and the computer prints out 14. I see its called the read-eval-print loop method. I need help converting that method into code and implemented.
Here is a simple calculator example to help get you started.
while True:
num1 = input('First Number: ')
num2 = input('Second Number: ')
op = input('Operator: ')
try:
num1 = int(num1)
num2 = int(num2)
except:
print('Input a valid number!')
continue
if op not in ['+', '-', '*']:
print('Invalid operator!')
continue
if op == '+':
print(str(num1 + num2))
elif op == '-':
print(str(num1 - num2))
elif op == '*':
print(str(num1 * num2))

Categories