0

Hello I am new to python and I am having trouble with this simple program that I am writing.

import random 
x = random.randint(0,100)
y = random.randint(0,100)
z = random.randint(0,100)
def calcavg(value1, value2,value3):
    total = (value1 + value2 + value3)/3
    return total 
avg = calcavg(x,y,z)
print(f" The Average of{x},{y}, and {z} is {avg}.")

On The last line of the code I am getting a syntax error on the {avg}.")

9
  • 5
    Are you using Python 2? That would result on a syntax error for print(...). Or are you using Python 3 < 3.6? That would result on a syntax error with print(f".."). I cannot reproduce any error on Python 3.8. Commented Sep 22, 2020 at 23:58
  • 5
    F-strings (i.e. f"hello {name}") were introduced in Python 3.6. Commented Sep 23, 2020 at 0:00
  • yea I am on codeRunner on Mac running the stock python and the python 3 option can't run anything I write for some reason. Commented Sep 23, 2020 at 0:11
  • 1
    Which version of Python are you using? Commented Sep 23, 2020 at 0:18
  • first check if {print("Hello, testing Python3")} is working or throwing an error. then please check below if that is the problem. stackoverflow.com/questions/19797616/… Commented Sep 23, 2020 at 0:18

1 Answer 1

-1
import random 
x = random.randint(0,100)
y = random.randint(0,100)
z = random.randint(0,100)
def calcavg(value1, value2,value3):
    total = (value1 + value2 + value3)/3
    return total 
avg = calcavg(x,y,z)
print(f"The Average of {x},{y}, and {z} is {avg}.")

works absolutely fine in PyCharm. Online compilers are failing.

Sign up to request clarification or add additional context in comments.

7 Comments

Why should OP use this? Please read How to Answer. We're not here to provide code to blindly copy and paste.
Sorry, first I wanted to give out the answer I tried and then looked for more resources on how to make it strong.
"I prefer the str.format() than the %-formatting"—OP isn't using % formatting. It is very likely that they simply need to use a newer version of Python.
{print(" The Average of %f,%f, and %f is %f"% (x,y,z,avg))} if OP wants to use %-formatting.
sorry to give a little more context I am in a beginners programming class in college and we are supposed to write this code using f strings
|

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.