cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1034
Views
0
Helpful
6
Replies

DevNet - Learning Labs - issues with the fortune cookie =-) - '->' in Python syntax error

_|brt.drml|_
Level 1
Level 1

I'm going through the Devnet learning track and I have an issue with the interpreter )> Apparently they would not recognize the '->'

When I run it - it hangs there with a syntax error.

I understand a syntax error, but it won't go away.

 

Any thoughts how to solve?

Tested this with windows cmd python, in git bash ...

 

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Script flow and debugging. Print your own fortune cookie!"""

 

import random

 

FORTUNES = [
" There is always good weather some day ",
" One day you will have a happy day ",
" Your kids would listen if you ask them something",
]

def generate_fortune() -> str:
return random.choice(FORTUNES)

def generate_lucky_numbers(how_many: int) -> list:
lucky_numbers = []
for _ in range(how_many:):
lucky_number..............................

 

1 Accepted Solution

Accepted Solutions
6 Replies 6

omz
VIP Alumni
VIP Alumni

Hi 

Can you share the error you are getting?

Check the version of Python you are using. 

The code works in Python3.

Screenshot 2020-05-08 at 09.56.15.png

Hi there,



The reply I get is:
$ py ./intro-python/part2/fortune_cookie.py
File "./intro-python/part2/fortune_cookie.py", line 16
def generate_fortune() -> str:
^
SyntaxError: invalid syntax



I ran this code within the windows command and yesterday this worked.

Today it does not. :) Weird and as a beginner I do not see the issue.

All information points that it is 'valid'..




Btw - tested this in python 2 & 3


Check your syntax you might have added or deleted something .. on line 13 .. do still have the closing bracket " ] " for the list? 

Does your line 9 - 18 look like this .. 

FORTUNES = [
    "There is a good chance your code will work, eventually.",
    "The weather will be hot, cold or just right today.",
    "I see Network DevOps in your future.",
]

def generate_fortune() -> str: """Use mystical forces (a random selection) to get a user's fortune.""" return random.choice(FORTUNES)

I can reproduce the same error if I remove the closing bracket

python3 fortune_cookie.py
  File "fortune_cookie.py", line 16
    def generate_fortune() -> str:
    ^
SyntaxError: invalid syntax

 

 

Hi There thank you for the quick reply,



I did try moving that bracket, but no success. Weird... I moved it up, to the left, indented it...

So no changes so far.

Weird, as I made already other code but always without the '->' ..



FORTUNES = [

"There is a good chance your code will work, eventually.",

"The weather will be hot, cold or just right today.",

"I see Network DevOps in your future.",

]





def generate_fortune() -> str:

"""Use mystical forces (a random selection) to get a user's fortune."""

return random.choice(FORTUNES)






Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: