Question

In: Computer Science

Hello! I'm trying to work on a python lab in my class, and we basically have...

Hello! I'm trying to work on a python lab in my class, and we basically have to make this method

play_interactive

Now for the fun bit. The function play_interactive will take just one argument --- the length of patterns to use as keys in the dictionary --- and will start an interactive game session, reading either '1' or '2' from the player as guesses, using the functions you wrote above and producing output as shown in the sample game session at the beginning of this writeup. If the player types in any other input (besides '1' or '2'), the game should terminate.

Hint: the input function can be used to read input from the user as a string.

My code so far looks like this, I have the basic loop down and working, but my guesses list to retain the guesses for this method isn't updating

def play_interactive(pattern_length=4):
while True:
x = input()
intX = int(x)
guesses = ([])
if(intX == 1 or intX == 2):
guesses.append(intX)
print(len(guesses))
else:
print("Finished")
break

Any help would be appreciated! Thank you so much

Solutions

Expert Solution

Error in your code was you were declaring the list inside while loop so every time loop runs, list got initialized and you are getting only one item in the list. Initialize the list before while loop, it will work.

Please use below modified code:

def play_interactive(pattern_length=4):
guesses = ([]) #initialize the list outside of while loop
while True:
x = input()
intX = int(x)
if(intX == 1 or intX == 2):
guesses.append(intX)
#below line you can remove, this is just for understanding and showing the output
print("length of the list is " )
print(len(guesses)) #print the length of the list
else:
print("Finished") #if input is not 1 or 2, print finish and exit
break
  
play_interactive() #driver call to the function

output:

Please rate your answer. Thanks


Related Solutions

Hello I'm stuck on my chemistry lab report. a. calculate the heat gained by the water...
Hello I'm stuck on my chemistry lab report. a. calculate the heat gained by the water in the calorimeter (qwater) after adding the aluminum. b. calculate the heat gained by the calorimeter (qcal) after adding aluminum. c. using results from questions a and b, calculate the heat lost by the aluminum (qmetal). d. calculate the specific heat capacity of aluminum in J/gk. e. determine percent error in the value of the heat capacity of aluminum by comparing it to its...
Invalid entry code in python my code is pasted below. The last elif statement, I'm trying...
Invalid entry code in python my code is pasted below. The last elif statement, I'm trying to get the program to print "invalid entry" if the entry for user_input is invalid. The first user prompt should only allow for numbers 1-10 and "exit" and "quit" import math user_prompt = """Enter number of operation that you want to execute <type exit or quit to end program>: 1 sin(x) 2 cos(x) 3 tan(x) 4 asin(x) 5 acos(x) 6 atan(x) 7 ln(x) 8...
Internal Resistance of a battery lab I'm doing this lab where we have to find the...
Internal Resistance of a battery lab I'm doing this lab where we have to find the internal resistance of a charged and uncharged battery. We got our data by plotting a V vs I graph, and found the internal resistance of each battery using the slopes of the graphs. From my graphs, I can see that the internal resistance of the charged battery is higher than that of the uncharged one. Which makes sense. However, I'm having a hard time...
What's wrong with my Python code. We have to find the regularexpression in Python My output...
What's wrong with my Python code. We have to find the regularexpression in Python My output should look like this My IP address 128. 0. 0. 1 My IP address 53. 20. 62. 201 My code ipAddresses = [] ipRegEx = re.compile(r"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}")    result = myRegEx.find all(Search Text)    def Regular_expression(ipAddresses)       for i in ipAddresses:          print(i) ipSearchResult = ipRegEx.search(line)    if ipSearchResult != None and ipSearchResult.group() not in ipAddresses:       ipAddresses.append(ipSearchResult.group()) we have to use loop too.
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
I'm having difficulty trying to make my code work. Create a subclass of Phone called SmartPhone....
I'm having difficulty trying to make my code work. Create a subclass of Phone called SmartPhone. Make this subclass have a no-arg constructor and a constructor that takes a name, email, and phone. Override the toString method to return the required output. Given Files: public class Demo1 { public static void test(Phone p) { System.out.println("Getter test:"); System.out.println(p.getName()); System.out.println(p.getNumber()); } public static void main(String[] args) { Phone test1 = new SmartPhone(); Phone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); System.out.println(test1); System.out.println(test2); System.out.println(test1);...
I'm working on a to-do list program in Python 2. I'm trying to delete an item...
I'm working on a to-do list program in Python 2. I'm trying to delete an item from the list and I'm not sure what I'm missing to do that. I haven't been able to get it to delete by string or by index number. Also, I'm trying to get the menu to run again after the user completes the add/delete/etc options. Do I need to put menu() menu_option = int(input("Welcome to your To-Do List. Please choose and option to continue:...
I'm having trouble with my do while loop. I'm trying to get it where if the...
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this? Here's my Code: import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new...
My physics class is working on Ohm's law and we did a lab about series and...
My physics class is working on Ohm's law and we did a lab about series and parallel circuits. While doing the lab report I got stuck on this question below. Please help! "What contributed to the percentage difference? In other words, account for sources of errors." This is my data: Part I Voltage (V) Current (A) Resistance (Ω) R = V/I % diff. 33 0.33A 100 0 45 0.45A 100 0 60 0.60A 100 0 75 0.75A 100 0 90...
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a...
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a dictionary, WITHOUT using pandas. I'm able to import pickle and can open my file...but I'm not sure how to create the dictionary.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT