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}.")

  • 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. – Gino Mempin Sep 22 '20 at 23:58
  • 5
    F-strings (i.e. `f"hello {name}"`) were introduced in Python 3.6. – John Gordon Sep 23 '20 at 00: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. – Jay Regensberger Sep 23 '20 at 00:11
  • 1
    Which version of Python are you using? – Daniel Walker Sep 23 '20 at 00:18
  • first check if {print("Hello, testing Python3")} is working or throwing an error. then please check below if that is the problem. https://stackoverflow.com/questions/19797616/coderunner-uses-old-2-71-version-of-python-instead-of-3-2-on-osx-10-7-5 – raja777m Sep 23 '20 at 00:18
  • The "stock python" is probably Python 2.7. If you need to use f-strings, then you need to use a Python 3.6+ (see [f-strings giving SyntaxError?](https://stackoverflow.com/q/50401632/2745495)). Also, Python 2.7 is already end-of-life, so you shouldn't be using it anyway. – Gino Mempin Sep 23 '20 at 00:21
  • Does this answer your question? [f-strings giving SyntaxError?](https://stackoverflow.com/questions/50401632/f-strings-giving-syntaxerror) – Gino Mempin Sep 23 '20 at 00:21
  • As they said, probably it's your python version, if you want to make it work with other versions try to use "format" method [here](https://www.geeksforgeeks.org/python-format-function/). so the print would be: ```print(' The Average of {},{}, and {} is {}.'.format(x, y, z, avg))``` – Federico A. Sep 23 '20 at 00:23
  • x.So I tried to change the format to python3 in the preferences and now all I get is the "xcrun: error: cannot be used within an App Sandbox." error. – Jay Regensberger Sep 23 '20 at 00:31

1 Answers1

-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.

raja777m
  • 401
  • 1
  • 8
  • 18
  • 1
    Why should OP use this? Please read [answer]. We're not here to provide code to blindly copy and paste. – ChrisGPT was on strike Sep 23 '20 at 00:01
  • Sorry, first I wanted to give out the answer I tried and then looked for more resources on how to make it strong. – raja777m Sep 23 '20 at 00:07
  • 1
    "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. – ChrisGPT was on strike Sep 23 '20 at 00:08
  • {print(" The Average of %f,%f, and %f is %f"% (x,y,z,avg))} if OP wants to use %-formatting. – raja777m Sep 23 '20 at 00:09
  • ...I'm not sure why this is so complicated. OP does _not_ want to use `%` formatting. This does not appear in the question, and is irrelevant. They are using _formatted string literals_ (f-strings). – ChrisGPT was on strike Sep 23 '20 at 00:10
  • 2
    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 – Jay Regensberger Sep 23 '20 at 00:13
  • works fine to me in PyCharm 3.8.2. Online compilers fail. edit an existing program and it works fine in this online compiler: https://www.geeksforgeeks.org/formatted-string-literals-f-strings-python/ – raja777m Sep 23 '20 at 00:16