spyder-ide / spyder

Official repository for Spyder - The Scientific Python Development Environment
https://www.spyder-ide.org
MIT License
8.32k stars 1.61k forks source link

SyntaxError located at a line that does not exist #22258

Closed manylettuces closed 3 months ago

manylettuces commented 3 months ago

Description

What steps will reproduce the problem?

Was writing code, suddenly Spyder claimed I had an 'unterminated triple-quoted string literal' located at a line that didn't exist (i.e. my code ended at around line 390, it claimed the error was at line 444). I tried to delete the chunks of the code to firgure out where exactly the error was, but even when I deleted all of the code on the file it still claimed I had the 'unterminated string literal'.

I'm not sure how to reproduce it - maybe I can paste the entire code?

Here it is:

6.0001 Problem Set 3

#

The 6.0001 Word Game

Created by: Kevin Luu and Jenna Wiens

#

Name :

Collaborators :

Time spent :

import math import random import string

VOWELS = 'aeiou' CONSONANTS = 'bcdfghjklmnpqrstvwxyz' HAND_SIZE = 7

SCRABBLE_LETTER_VALUES = { 'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10, '*': 0}

-----------------------------------

Helper code

(you don't need to understand this helper code)

WORDLIST_FILENAME = "words.txt"

def load_words(): """ Returns a list of valid words. Words are strings of lowercase letters.

Depending on the size of the word list, this function may
take a while to finish.
"""

print("Loading word list from file...")
# inFile: file
inFile = open(WORDLIST_FILENAME, 'r')
# wordlist: list of strings
wordlist = []
for line in inFile:
    wordlist.append(line.strip().lower())
print("  ", len(wordlist), "words loaded.")
return wordlist

def get_frequency_dict(sequence): """ Returns a dictionary where the keys are elements of the sequence and the values are integer counts, for the number of times that an element is repeated in the sequence.

sequence: string or list
return: dictionary
"""

# freqs: dictionary (element_type -> int)
freq = {}
for x in sequence:
    freq[x] = freq.get(x,0) + 1
return freq

(end of helper code)

-----------------------------------

word_list = load_words()

#

Problem #1: Scoring a word

# def get_word_score(word, n): """ Returns the score for a word. Assumes the word is a valid word.

You may assume that the input word is always either a string of letters, 
or the empty string "". You may not assume that the string will only contain 
lowercase letters, so you will have to handle uppercase and mixed case strings 
appropriately. 

The score for a word is the product of two components:

The first component is the sum of the points for letters in the word.
The second component is the larger of:
        1, or
        7*wordlen - 3*(n-wordlen), where wordlen is the length of the word
        and n is the hand length when the word was played

Letters are scored as in Scrabble; A is worth 1, B is
worth 3, C is worth 3, D is worth 2, E is worth 1, and so on.

word: string
n: int >= 0
returns: int >= 0
"""
score = 0
word = word.lower()
for char in word:
    x = int(SCRABBLE_LETTER_VALUES[char])
    score += x 
score2 = (7 * len(word)) - (3 * (n - len(word)))
if score2 >= 1:
    score *= score2
return score

#

Make sure you understand how this function works and what it does!

# def display_hand(hand): """ Displays the letters currently in the hand.

For example:
   display_hand({'a':1, 'x':2, 'l':3, 'e':1})
Should print out something like:
   a x x l l l e
The order of the letters is unimportant.

hand: dictionary (string -> int)
"""

for letter in hand.keys():
    for j in range(hand[letter]):
         print(letter, end=' ')      # print all on the same line
print()                              # print an empty line

#

Make sure you understand how this function works and what it does!

You will need to modify this for Problem #4.

# def deal_hand(n): """ Returns a random hand containing n lowercase letters. ceil(n/3) letters in the hand should be VOWELS (note, ceil(n/3) means the smallest integer not less than n/3).

Hands are represented as dictionaries. The keys are
letters and the values are the number of times the
particular letter is repeated in that hand.

n: int >= 0
returns: dictionary (string -> int)
"""

hand={}
num_vowels = int(math.ceil(n / 3)) - 1

for i in range(num_vowels):
    x = random.choice(VOWELS)
    hand[x] = hand.get(x, 0) + 1

for i in range(num_vowels, n):    
    x = random.choice(CONSONANTS)
    hand[x] = hand.get(x, 0) + 1

hand['*'] = 1

return hand

#

Problem #2: Update a hand by removing letters

# def update_hand(hand, word): """ Does NOT assume that hand contains every letter in word at least as many times as the letter appears in word. Letters in word that don't appear in hand should be ignored. Letters that appear in word more times than in hand should never result in a negative count; instead, set the count in the returned hand to 0 (or remove the letter from the dictionary, depending on how your code is structured).

Updates the hand: uses up the letters in the given word
and returns the new hand, without those letters in it.

Has no side effects: does not modify hand.

word: string
hand: dictionary (string -> int)    
returns: dictionary (string -> int)
"""
new_hand = hand.copy()
word1 = word.lower()
for char in word1:
    if char in hand.copy():
        if new_hand[char] == 0:
            continue
        else:
            new_hand[char] -= 1
return new_hand

#

Problem #3: Test word validity

# def is_valid_word(word, hand, word_list): """ Returns True if word is in the word_list and is entirely composed of letters in the hand. Otherwise, returns False. Does not mutate hand or word_list.

word: string
hand: dictionary (string -> int)
word_list: list of lowercase strings
returns: boolean
"""
word1 = word.lower()
new_hand = hand.copy()
wildcard = False
for char in word1:
    if char not in hand.copy():
        return False
    else:
        new_hand[char] -= 1
        if new_hand[char] < 0:
            return False
    if char == '*':
        wildcard = True
vowels = ['a', 'e', 'i', 'o', 'u']
if wildcard == True:
    for e in vowels:
        word2 = word1.replace('*', e)
        for e in word_list:
            if e == word2:
                return True
    else:
        return False
else:
    for e in word_list:
        if e == word1:
            return True
    else:
        return False

#

Problem #5: Playing a hand

# def calculate_handlen(hand): """ Returns the length (number of letters) in the current hand.

hand: dictionary (string-> int)
returns: integer
"""

pass  # TO DO... Remove this line when you implement this function

def play_hand(hand, word_list):

"""
Allows the user to play the given hand, as follows:

* The hand is displayed.

* The user may input a word.

* When any word is entered (valid or invalid), it uses up letters
  from the hand.

* An invalid word is rejected, and a message is displayed asking
  the user to choose another word.

* After every valid word: the score for that word is displayed,
  the remaining letters in the hand are displayed, and the user
  is asked to input another word.

* The sum of the word scores is displayed when the hand finishes.

* The hand finishes when there are no more unused letters.
  The user can also finish playing the hand by inputing two 
  exclamation points (the string '!!') instead of a word.

  hand: dictionary (string -> int)
  word_list: list of lowercase strings
  returns: the total score for the hand

"""

# BEGIN PSEUDOCODE <-- Remove this comment when you implement this function
# Keep track of the total score

# As long as there are still letters left in the hand:

    # Display the hand

    # Ask user for input

    # If the input is two exclamation points:

        # End the game (break out of the loop)

    # Otherwise (the input is not two exclamation points):

        # If the word is valid:

            # Tell the user how many points the word earned,
            # and the updated total score

        # Otherwise (the word is not valid):
            # Reject invalid word (print a message)

        # update the user's hand by removing the letters of their inputted word

# Game is over (user entered '!!' or ran out of letters),
# so tell user the total score

# Return the total score as result of function

#

Problem #6: Playing a game

#

procedure you will use to substitute a letter in a hand

#

def substitute_hand(hand, letter): """ Allow the user to replace all copies of one letter in the hand (chosen by user) with a new letter chosen from the VOWELS and CONSONANTS at random. The new letter should be different from user's choice, and should not be any of the letters already in the hand.

If user provide a letter not in the hand, the hand should be the same.

Has no side effects: does not mutate hand.

For example:
    substitute_hand({'h':1, 'e':1, 'l':2, 'o':1}, 'l')
might return:
    {'h':1, 'e':1, 'o':1, 'x':2} -> if the new letter is 'x'
The new letter should not be 'h', 'e', 'l', or 'o' since those letters were
already in the hand.

hand: dictionary (string -> int)
letter: string
returns: dictionary (string -> int)
"""

pass  # TO DO... Remove this line when you implement this function

def play_game(word_list): """ Allow the user to play a series of hands

* Asks the user to input a total number of hands

* Accumulates the score for each hand into a total score for the 
  entire series

* For each hand, before playing, ask the user if they want to substitute
  one letter for another. If the user inputs 'yes', prompt them for their
  desired letter. This can only be done once during the game. Once the
  substitue option is used, the user should not be asked if they want to
  substitute letters in the future.

* For each hand, ask the user if they would like to replay the hand.
  If the user inputs 'yes', they will replay the hand and keep 
  the better of the two scores for that hand.  This can only be done once 
  during the game. Once the replay option is used, the user should not
  be asked if they want to replay future hands. Replaying the hand does
  not count as one of the total number of hands the user initially
  wanted to play.

        * Note: if you replay a hand, you do not get the option to substitute
                a letter - you must play whatever hand you just had.

* Returns the total score for the series of hands

word_list: list of lowercase strings
"""

print("play_game not implemented.") # TO DO... Remove this line when you implement this function

#

Build data structures used for entire session and play game

Do not remove the "if name == 'main':" line - this code is executed

when the program is run directly, instead of through an import statement

# if name == 'main': word_list = load_words() play_game(word_list) for is_valid_word """ failure=False

# test 1
hand = {'a': 1, 'r': 1, 'e': 1, 'j': 2, 'm': 1, '*': 1}
word = "e*m"

if is_valid_word(word, hand, word_list):
    print("FAILURE: test_is_valid_word() with wildcards")
    print("\tExpected False, but got True for word: '" + word + "' and hand:", hand)

    failure = True

# test 2
hand = {'n': 1, 'h': 1, '*': 1, 'y': 1, 'd':1, 'w':1, 'e': 2}
word = "honey"

if is_valid_word(word, hand, word_list):
    print("FAILURE: test_is_valid_word() with wildcards")
    print("\tExpected False, but got True for word: '"+ word +"' and hand:", hand)

    failure = True

# test 3
hand = {'n': 1, 'h': 1, '*': 1, 'y': 1, 'd':1, 'w':1, 'e': 2}
word = "h*ney"

if not is_valid_word(word, hand, word_list):
    print("FAILURE: test_is_valid_word() with wildcards")
    print("\tExpected True, but got False for word: '"+ word +"' and hand:", hand)

    failure = True

# test 4
hand = {'c': 1, 'o': 1, '*': 1, 'w': 1, 's':1, 'z':1, 'y': 2}
word = "c*wz"

if is_valid_word(word, hand, word_list):
    print("FAILURE: test_is_valid_word() with wildcards")
    print("\tExpected False, but got True for word: '"+ word +"' and hand:", hand)

    failure = True    

# dictionary of words and scores WITH wildcards
words = {("h*ney", 7):290, ("c*ws", 6):176, ("wa*ls", 7):203}
for (word, n) in words.keys():
    score = get_word_score(word, n)
    if score != words[(word, n)]:
        print("FAILURE: test_get_word_score() with wildcards")
        print("\tExpected", words[(word, n)], "points but got '" + \
              str(score) + "' for word '" + word + "', n=" + str(n))
        failure=True      

if not failure:
    print("SUCCESS: test_wildcard()"

Traceback

Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero

QTextCursor::setPosition: Position '-1' out of range
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero

Versions

Dependencies

# Mandatory:
atomicwrites >=1.2.0                             :  1.4.0 (OK)
chardet >=2.0.0                                  :  4.0.0 (OK)
cloudpickle >=0.5.0                              :  2.2.1 (OK)
cookiecutter >=1.6.0                             :  2.5.0 (OK)
diff_match_patch >=20181111                      :  20200713 (OK)
intervaltree >=3.0.2                             :  3.1.0 (OK)
IPython >=7.31.1,<9.0.0,!=8.8.0,!=8.9.0,!=8.10.0 :  8.20.0 (OK)
jedi >=0.17.2,<0.19.0                            :  0.18.1 (OK)
jellyfish >=0.7                                  :  1.0.1 (OK)
jsonschema >=3.2.0                               :  4.19.2 (OK)
keyring >=17.0.0                                 :  23.13.1 (OK)
nbconvert >=4.0                                  :  7.10.0 (OK)
numpydoc >=0.6.0                                 :  1.5.0 (OK)
paramiko >=2.4.0                                 :  2.8.1 (OK)
parso >=0.7.0,<0.9.0                             :  0.8.3 (OK)
pexpect >=4.4.0                                  :  4.8.0 (OK)
pickleshare >=0.4                                :  0.7.5 (OK)
psutil >=5.3                                     :  5.9.0 (OK)
pygments >=2.0                                   :  2.15.1 (OK)
pylint >=2.5.0,<3.0                              :  2.16.2 (OK)
pylint_venv >=2.1.1                              :  2.3.0 (OK)
pyls_spyder >=0.4.0                              :  0.4.0 (OK)
pylsp >=1.7.2,<1.8.0                             :  1.7.2 (OK)
pylsp_black >=1.2.0                              :  1.2.1 (OK)
qdarkstyle >=3.0.2,<3.2.0                        :  3.0.2 (OK)
qstylizer >=0.2.2                                :  0.2.2 (OK)
qtawesome >=1.2.1                                :  1.2.2 (OK)
qtconsole >=5.4.2,<5.5.0                         :  5.4.2 (OK)
qtpy >=2.1.0                                     :  2.4.1 (OK)
rtree >=0.9.7                                    :  1.0.1 (OK)
setuptools >=49.6.0                              :  68.2.2 (OK)
sphinx >=0.6.6                                   :  5.0.2 (OK)
spyder_kernels >=2.4.3,<2.5.0                    :  2.4.4 (OK)
textdistance >=4.2.0                             :  4.2.1 (OK)
three_merge >=0.1.1                              :  0.1.1 (OK)
watchdog >=0.10.3                                :  2.1.6 (OK)
zmq >=22.1.0                                     :  25.1.2 (OK)

# Optional:
cython >=0.21                                    :  None (NOK)
matplotlib >=3.0.0                               :  3.8.0 (OK)
numpy >=1.7                                      :  1.26.4 (OK)
pandas >=1.1.1                                   :  2.1.4 (OK)
scipy >=0.17.0                                   :  1.11.4 (OK)
sympy >=0.7.3                                    :  1.12 (OK)
manylettuces commented 3 months ago

Description

What steps will reproduce the problem?

Was writing code, suddenly Spyder claimed I had an 'unterminated triple-quoted string literal' located at a line that didn't exist (i.e. my code ended at around line 390, it claimed the error was at line 444). I tried to delete the chunks of the code to firgure out where exactly the error was, but even when I deleted all of the code on the file it still claimed I had the 'unterminated string literal'.

I'm not sure how to reproduce it - maybe I can paste the entire code?

Here it is:

6.0001 Problem Set 3

The 6.0001 Word Game

Created by: Kevin Luu and Jenna Wiens

Name :

Collaborators :

Time spent :

import math import random import string

VOWELS = 'aeiou' CONSONANTS = 'bcdfghjklmnpqrstvwxyz' HAND_SIZE = 7

SCRABBLE_LETTER_VALUES = { 'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10, '*': 0}

-----------------------------------

Helper code

(you don't need to understand this helper code)

WORDLIST_FILENAME = "words.txt"

def load_words(): """ Returns a list of valid words. Words are strings of lowercase letters.

Depending on the size of the word list, this function may
take a while to finish.
"""

print("Loading word list from file...")
# inFile: file
inFile = open(WORDLIST_FILENAME, 'r')
# wordlist: list of strings
wordlist = []
for line in inFile:
    wordlist.append(line.strip().lower())
print("  ", len(wordlist), "words loaded.")
return wordlist

def get_frequency_dict(sequence): """ Returns a dictionary where the keys are elements of the sequence and the values are integer counts, for the number of times that an element is repeated in the sequence.

sequence: string or list
return: dictionary
"""

# freqs: dictionary (element_type -> int)
freq = {}
for x in sequence:
    freq[x] = freq.get(x,0) + 1
return freq

(end of helper code)

-----------------------------------

word_list = load_words()

Problem #1: Scoring a word

def get_word_score(word, n): """ Returns the score for a word. Assumes the word is a valid word.

You may assume that the input word is always either a string of letters, 
or the empty string "". You may not assume that the string will only contain 
lowercase letters, so you will have to handle uppercase and mixed case strings 
appropriately. 

The score for a word is the product of two components:

The first component is the sum of the points for letters in the word.
The second component is the larger of:
        1, or
        7*wordlen - 3*(n-wordlen), where wordlen is the length of the word
        and n is the hand length when the word was played

Letters are scored as in Scrabble; A is worth 1, B is
worth 3, C is worth 3, D is worth 2, E is worth 1, and so on.

word: string
n: int >= 0
returns: int >= 0
"""
score = 0
word = word.lower()
for char in word:
    x = int(SCRABBLE_LETTER_VALUES[char])
    score += x 
score2 = (7 * len(word)) - (3 * (n - len(word)))
if score2 >= 1:
    score *= score2
return score

Make sure you understand how this function works and what it does!

def display_hand(hand): """ Displays the letters currently in the hand.

For example:
   display_hand({'a':1, 'x':2, 'l':3, 'e':1})
Should print out something like:
   a x x l l l e
The order of the letters is unimportant.

hand: dictionary (string -> int)
"""

for letter in hand.keys():
    for j in range(hand[letter]):
         print(letter, end=' ')      # print all on the same line
print()                              # print an empty line

Make sure you understand how this function works and what it does!

You will need to modify this for Problem #4.

def deal_hand(n): """ Returns a random hand containing n lowercase letters. ceil(n/3) letters in the hand should be VOWELS (note, ceil(n/3) means the smallest integer not less than n/3).

Hands are represented as dictionaries. The keys are
letters and the values are the number of times the
particular letter is repeated in that hand.

n: int >= 0
returns: dictionary (string -> int)
"""

hand={}
num_vowels = int(math.ceil(n / 3)) - 1

for i in range(num_vowels):
    x = random.choice(VOWELS)
    hand[x] = hand.get(x, 0) + 1

for i in range(num_vowels, n):    
    x = random.choice(CONSONANTS)
    hand[x] = hand.get(x, 0) + 1

hand['*'] = 1

return hand

Problem #2: Update a hand by removing letters

def update_hand(hand, word): """ Does NOT assume that hand contains every letter in word at least as many times as the letter appears in word. Letters in word that don't appear in hand should be ignored. Letters that appear in word more times than in hand should never result in a negative count; instead, set the count in the returned hand to 0 (or remove the letter from the dictionary, depending on how your code is structured).

Updates the hand: uses up the letters in the given word
and returns the new hand, without those letters in it.

Has no side effects: does not modify hand.

word: string
hand: dictionary (string -> int)    
returns: dictionary (string -> int)
"""
new_hand = hand.copy()
word1 = word.lower()
for char in word1:
    if char in hand.copy():
        if new_hand[char] == 0:
            continue
        else:
            new_hand[char] -= 1
return new_hand

Problem #3: Test word validity

def is_valid_word(word, hand, word_list): """ Returns True if word is in the word_list and is entirely composed of letters in the hand. Otherwise, returns False. Does not mutate hand or word_list.

word: string
hand: dictionary (string -> int)
word_list: list of lowercase strings
returns: boolean
"""
word1 = word.lower()
new_hand = hand.copy()
wildcard = False
for char in word1:
    if char not in hand.copy():
        return False
    else:
        new_hand[char] -= 1
        if new_hand[char] < 0:
            return False
    if char == '*':
        wildcard = True
vowels = ['a', 'e', 'i', 'o', 'u']
if wildcard == True:
    for e in vowels:
        word2 = word1.replace('*', e)
        for e in word_list:
            if e == word2:
                return True
    else:
        return False
else:
    for e in word_list:
        if e == word1:
            return True
    else:
        return False

Problem #5: Playing a hand

def calculate_handlen(hand): """ Returns the length (number of letters) in the current hand.

hand: dictionary (string-> int)
returns: integer
"""

pass  # TO DO... Remove this line when you implement this function

def play_hand(hand, word_list):

"""
Allows the user to play the given hand, as follows:

* The hand is displayed.

* The user may input a word.

* When any word is entered (valid or invalid), it uses up letters
  from the hand.

* An invalid word is rejected, and a message is displayed asking
  the user to choose another word.

* After every valid word: the score for that word is displayed,
  the remaining letters in the hand are displayed, and the user
  is asked to input another word.

* The sum of the word scores is displayed when the hand finishes.

* The hand finishes when there are no more unused letters.
  The user can also finish playing the hand by inputing two 
  exclamation points (the string '!!') instead of a word.

  hand: dictionary (string -> int)
  word_list: list of lowercase strings
  returns: the total score for the hand

"""

# BEGIN PSEUDOCODE <-- Remove this comment when you implement this function
# Keep track of the total score

# As long as there are still letters left in the hand:

    # Display the hand

    # Ask user for input

    # If the input is two exclamation points:

        # End the game (break out of the loop)

    # Otherwise (the input is not two exclamation points):

        # If the word is valid:

            # Tell the user how many points the word earned,
            # and the updated total score

        # Otherwise (the word is not valid):
            # Reject invalid word (print a message)

        # update the user's hand by removing the letters of their inputted word

# Game is over (user entered '!!' or ran out of letters),
# so tell user the total score

# Return the total score as result of function

Problem #6: Playing a game

procedure you will use to substitute a letter in a hand

def substitute_hand(hand, letter): """ Allow the user to replace all copies of one letter in the hand (chosen by user) with a new letter chosen from the VOWELS and CONSONANTS at random. The new letter should be different from user's choice, and should not be any of the letters already in the hand.

If user provide a letter not in the hand, the hand should be the same.

Has no side effects: does not mutate hand.

For example:
    substitute_hand({'h':1, 'e':1, 'l':2, 'o':1}, 'l')
might return:
    {'h':1, 'e':1, 'o':1, 'x':2} -> if the new letter is 'x'
The new letter should not be 'h', 'e', 'l', or 'o' since those letters were
already in the hand.

hand: dictionary (string -> int)
letter: string
returns: dictionary (string -> int)
"""

pass  # TO DO... Remove this line when you implement this function

def play_game(word_list): """ Allow the user to play a series of hands

* Asks the user to input a total number of hands

* Accumulates the score for each hand into a total score for the 
  entire series

* For each hand, before playing, ask the user if they want to substitute
  one letter for another. If the user inputs 'yes', prompt them for their
  desired letter. This can only be done once during the game. Once the
  substitue option is used, the user should not be asked if they want to
  substitute letters in the future.

* For each hand, ask the user if they would like to replay the hand.
  If the user inputs 'yes', they will replay the hand and keep 
  the better of the two scores for that hand.  This can only be done once 
  during the game. Once the replay option is used, the user should not
  be asked if they want to replay future hands. Replaying the hand does
  not count as one of the total number of hands the user initially
  wanted to play.

        * Note: if you replay a hand, you do not get the option to substitute
                a letter - you must play whatever hand you just had.

* Returns the total score for the series of hands

word_list: list of lowercase strings
"""

print("play_game not implemented.") # TO DO... Remove this line when you implement this function

Build data structures used for entire session and play game

Do not remove the "if name == 'main':" line - this code is executed

when the program is run directly, instead of through an import statement

if name == 'main': word_list = load_words() play_game(word_list) for is_valid_word """ failure=False

# test 1
hand = {'a': 1, 'r': 1, 'e': 1, 'j': 2, 'm': 1, '*': 1}
word = "e*m"

if is_valid_word(word, hand, word_list):
    print("FAILURE: test_is_valid_word() with wildcards")
    print("\tExpected False, but got True for word: '" + word + "' and hand:", hand)

    failure = True

# test 2
hand = {'n': 1, 'h': 1, '*': 1, 'y': 1, 'd':1, 'w':1, 'e': 2}
word = "honey"

if is_valid_word(word, hand, word_list):
    print("FAILURE: test_is_valid_word() with wildcards")
    print("\tExpected False, but got True for word: '"+ word +"' and hand:", hand)

    failure = True

# test 3
hand = {'n': 1, 'h': 1, '*': 1, 'y': 1, 'd':1, 'w':1, 'e': 2}
word = "h*ney"

if not is_valid_word(word, hand, word_list):
    print("FAILURE: test_is_valid_word() with wildcards")
    print("\tExpected True, but got False for word: '"+ word +"' and hand:", hand)

    failure = True

# test 4
hand = {'c': 1, 'o': 1, '*': 1, 'w': 1, 's':1, 'z':1, 'y': 2}
word = "c*wz"

if is_valid_word(word, hand, word_list):
    print("FAILURE: test_is_valid_word() with wildcards")
    print("\tExpected False, but got True for word: '"+ word +"' and hand:", hand)

    failure = True    

# dictionary of words and scores WITH wildcards
words = {("h*ney", 7):290, ("c*ws", 6):176, ("wa*ls", 7):203}
for (word, n) in words.keys():
    score = get_word_score(word, n)
    if score != words[(word, n)]:
        print("FAILURE: test_get_word_score() with wildcards")
        print("\tExpected", words[(word, n)], "points but got '" + \
              str(score) + "' for word '" + word + "', n=" + str(n))
        failure=True      

if not failure:
    print("SUCCESS: test_wildcard()"

Traceback

Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero

QTextCursor::setPosition: Position '-1' out of range
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\jell2\anaconda3\Lib\site-packages\spyder\plugins\editor\panels\scrollflag.py", line 176, in paintEvent
    scale_factor = groove_rect.height() / self.get_scrollbar_value_height()
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero

Versions

  • Spyder version: 5.4.3 (conda)
  • Python version: 3.11.7 64-bit
  • Qt version: 5.15.2
  • PyQt5 version: 5.15.10
  • Operating System: Windows 10

Dependencies

# Mandatory:
atomicwrites >=1.2.0                             :  1.4.0 (OK)
chardet >=2.0.0                                  :  4.0.0 (OK)
cloudpickle >=0.5.0                              :  2.2.1 (OK)
cookiecutter >=1.6.0                             :  2.5.0 (OK)
diff_match_patch >=20181111                      :  20200713 (OK)
intervaltree >=3.0.2                             :  3.1.0 (OK)
IPython >=7.31.1,<9.0.0,!=8.8.0,!=8.9.0,!=8.10.0 :  8.20.0 (OK)
jedi >=0.17.2,<0.19.0                            :  0.18.1 (OK)
jellyfish >=0.7                                  :  1.0.1 (OK)
jsonschema >=3.2.0                               :  4.19.2 (OK)
keyring >=17.0.0                                 :  23.13.1 (OK)
nbconvert >=4.0                                  :  7.10.0 (OK)
numpydoc >=0.6.0                                 :  1.5.0 (OK)
paramiko >=2.4.0                                 :  2.8.1 (OK)
parso >=0.7.0,<0.9.0                             :  0.8.3 (OK)
pexpect >=4.4.0                                  :  4.8.0 (OK)
pickleshare >=0.4                                :  0.7.5 (OK)
psutil >=5.3                                     :  5.9.0 (OK)
pygments >=2.0                                   :  2.15.1 (OK)
pylint >=2.5.0,<3.0                              :  2.16.2 (OK)
pylint_venv >=2.1.1                              :  2.3.0 (OK)
pyls_spyder >=0.4.0                              :  0.4.0 (OK)
pylsp >=1.7.2,<1.8.0                             :  1.7.2 (OK)
pylsp_black >=1.2.0                              :  1.2.1 (OK)
qdarkstyle >=3.0.2,<3.2.0                        :  3.0.2 (OK)
qstylizer >=0.2.2                                :  0.2.2 (OK)
qtawesome >=1.2.1                                :  1.2.2 (OK)
qtconsole >=5.4.2,<5.5.0                         :  5.4.2 (OK)
qtpy >=2.1.0                                     :  2.4.1 (OK)
rtree >=0.9.7                                    :  1.0.1 (OK)
setuptools >=49.6.0                              :  68.2.2 (OK)
sphinx >=0.6.6                                   :  5.0.2 (OK)
spyder_kernels >=2.4.3,<2.5.0                    :  2.4.4 (OK)
textdistance >=4.2.0                             :  4.2.1 (OK)
three_merge >=0.1.1                              :  0.1.1 (OK)
watchdog >=0.10.3                                :  2.1.6 (OK)
zmq >=22.1.0                                     :  25.1.2 (OK)

# Optional:
cython >=0.21                                    :  None (NOK)
matplotlib >=3.0.0                               :  3.8.0 (OK)
numpy >=1.7                                      :  1.26.4 (OK)
pandas >=1.1.1                                   :  2.1.4 (OK)
scipy >=0.17.0                                   :  1.11.4 (OK)
sympy >=0.7.3                                    :  1.12 (OK)

Never mind, the code wasn't showing up properly on my screen. I have found & solved the issue after copying & pasting the entire code a second time.