My program only appends one user input to list when main is looped

2024/11/24 4:47:41

This is part of my code for a guessing game. I want to count the guesses of a player, and then append their name and number of guesses to a list that is later written or appended to file. As of now, it only appends the last player of the game to the list and file, not all the players. What do I do wrong?

from typing import List, Tuple
def choose:
'"code""""""guesses = 0while choose(): guesses += 1 passname = input('What is your name?:')highscore: List[Tuple[str, int]] = []highscore.append((name, guesses))sorting_by_second = sorted(highscore, key=lambda X: X[1])with open('highscore.txt', 'w') as f:for name, guesses in sorting_by_second:f.write(f'{name} guess {guesses}.\n')new_player = input('New player?(yes/no:').lower()if new_player == 'yes':main()else:breakmain()
Answer

The 'w' mode overwrites highscore.txt. Think of it as deleting the file and then creating it all over again.

You want open('highscore.txt', 'a') to append, as described in https://docs.python.org/3/library/functions.html#open

EDIT:

Alternatively, perhaps you want to initialize highscore to the empty list outside the loop? (Your question is unclear. It would be helpful to replace the current code with valid python code, and to show example desired file output.)

As it stands, you loop many times, each time making the length of highscore zero, then appending so length is one, then you sort a single entry (no-op), then you write a one-line file. It isn't clear if you're trying to write e.g. a three-line file by interacting and looping three times, or by invoking the program from bash three separate times (in which case 'a' append would be appropriate). Help us out, and clarify the code and your intent.

Suppose Alice and Bob are playing. Perhaps instead of a list you would prefer to use a name_to_score dict, if you intend to overwrite Alice's score each time she plays again.

You don't show us the content of def choose but I assume it prompts Yes/No and returns True/False. You don't show us the content of def main but I assume that you do not want to invoke main() in the body of the loop, since the while choose(): loop probably suffices. Trying to read your mind doesn't always work well. Stating your problem more clearly would help us give more relevant answers.

https://en.xdnf.cn/q/120572.html

Related Q&A

How to split a python dictionary for its values on matching a key

my_dict1 = {a:1, chk:{b:2, c:3}, e:{chk:{f:5, g:6}} }I would like to loop through the dict recursively and if the key is chk, split it. Expected output:{a:1, b:2, e:{f:5}} {a:1, c:3, e:{f:5}} {a:1, b:2…

PyException: ImportError: No module named domreg

I am getting the below error while running this script ("from xml.dom import minidom") from chaquopy androi application python console. PyException: ImportError: No module named domreg

Plotting polynomial with given coefficients

Im trying to plot a polynomial with coefficients given in array:input: [an,a(n-1),...,a0] output: plot of polynomial anx^n + a(n-1)x^(n-1) + ... + a0I would like to use matplotlib polt() function so I…

grouping data using unique combinations

n my below data set, I need to find unique sequences and assign them a serial no ..DataSet :user age maritalstatus product A Young married 111 B young married 222 C young Single 111 D…

Python - Sorting in ascending order in a txt file

I had a huge document that I parsed using regex to give a txt file (json.dump) similar to the following:{"stuff": [{"name": ["frfer", "niddsi", ], "number&q…

How to make setuptools install a wheel containing multiple packages?

Suppose this wheel:M Filemode Length Date Time File - ---------- -------- ----------- -------- --------------------------------------------rw-rw-r-- 1358 26-Sep-2018 21:08…

Python - Randomly select words to display in a quiz [duplicate]

This question already has answers here:How can I randomly select (choose) an item from a list (get a random element)?(18 answers)Closed 9 years ago.Im in need of some help. Here we have my two lists:w…

How to extract only the english words from the list?

I tried to extract only the English words from the following list: l = [0, b, x14, x00, x1fP, xe0O, xd0, xea, i, x10, xa2, xd8, x08, x00, 00, x9d, x14, x00, x80, xcc, xbf, xb4, xdbLB, xb0, x7f, xe9, x9…

Javascript variable with html code regex email matching

This python script is not working to output the email address [email protected] for this case.This was my previous post.How can I use BeautifulSoup or Slimit on a site to output the email address from …