talkpython / python-for-absolute-beginners-course

Code samples and other handouts for our course.
https://training.talkpython.fm/courses/explore_beginners/python-for-absolute-beginners
MIT License
2.3k stars 1k forks source link

07-word-completion-basic-edition #10

Closed GeekDGirl closed 4 years ago

GeekDGirl commented 4 years ago

I have been working on this addition to the RPS game. I have installed prompt-toolkit 3.0.5. As I have been coding along with you, I have received the following error: File "/PycharmProjects/RPS/RPS-v8.py", line 157 word_comp: object = word_completer(roll_names) ^ SyntaxError: invalid syntax

I can't figure out what is causing the error as I have attempted to follow your code...there were some differences that came up when I entered the WordCompleter...the autofill had me use word_completer for example. Below is the section of code that relates:

def get_roll(player_name, roll_names): print(f"Available rolls: {', '.join(roll_names)}"

for index, r in enumerate(roll_names, start=1):

#     print(f"{index}. {r}")

word_comp = word_completer(roll_names)
roll = prompt(f"{player_name}, what is your roll: ", completer=word_comp)

text = input(f"{player_name}, what is your roll?")
selected_index = int(text) - 1

if not roll or roll not in roll_names:
    print(f"Sorry {player_name}, {roll} is not valid!")
    return None

return roll
grze744 commented 4 years ago

Hi! I also had some problems with this lesson recently. It's much more advanced than the stuff before and I'm not sure about some stuff there. Definitely not something that I could replicate myself. Perhaps studing documentary will help me, but I'm not used to it. I think that I can help you though. WordCompleter is function imported from Prompt Toolkit library and it's need to be written excatly to the letter, with capital letters and without spaces. It seems that you wrote "word_completer" instead. Try using "WordCompleter" and see if it works. It fifth line of the code you posted, just after comments with excluded code.

grze744 commented 4 years ago

Ah, it looks like you already know that! Sorry for misunderstaing. Perhaps you should ignore the correction and force WordCompleter there? I don't have this problem, PyCharm completes it correctly for me, and I have the same version of library.

GeekDGirl commented 4 years ago

I went in and made the changes to use WordCompleter. I'm still throwing an error:

File "/PycharmProjects/RPS/RPS-v8.py", line 157 word_comp = WordCompleter(roll_names) ^ SyntaxError: invalid syntax

The code that is involved is below:

def get_roll(player_name, roll_names): print(f"Available rolls: {', '.join(roll_names)}."

for index, r in enumerate(roll_names, start=1):

#     print(f"{index}. {r}")

word_comp = WordCompleter(roll_names)
roll = prompt(f"{player_name}, what is your roll: ", completer=word_comp)

text = input(f"{player_name}, what is your roll?")
selected_index = int(text) - 1

if not roll or roll not in roll_names:
    print(f"Sorry {player_name}, {roll} is not valid!")
    return None

return roll
grze744 commented 4 years ago

Are you sure you have the library imported? Looks like it doesn't know that WordCompleter is a function and only functions can have parenthesis. Remember to try the code in Python Shell and not in PyCharm. For that you also need to open the file in virtual environment with the library installed. Perhaps you're somewhat executing the file in another environment where there is no prompt-toolkit? Try running the code in PyCharm terminal, you should get some other error if this is the case (because you apparently installed the library using PyCharm but it can't show prompts).

GeekDGirl commented 4 years ago

Yes, I have the library imported. At this point, I'm still running the program in PyCharm because it's throwing the error on the variable word_comp.

grze744 commented 4 years ago

It will not work in PyCharm. Try the code in shell, perhaps there is no problem here. Mike tells about what to do about PyCharm thing in lecture 7 in chapter 10, starting around 05:15 minute: https://training.talkpython.fm/player/course/python-for-absolute-beginners/lecture/241007

mikeckennedy commented 4 years ago

Hi folks. You can't use the fancy terminal tools like prompt-toolkit within PyCharm because it's not a real terminal. Just copy the run command you see pycharm using when you press go and past it into a ternimal / command prompt.