-1

What is the Best Thing - Tom Scott

basically trying to imitate this code in python to make code that ranks items based on the users input.

output 2 items from a list, ask user for which is better, add +1 score to that item, add -1 score to the other item, move on to next comparison.

new to coding, so i can understand basics but i don't understand the best way to do this.

import random
file = open('items.txt', 'r')
read = file.readlines()
modified = []
for line in read:
  if line[-1] == '\n':
    modified.append(line[:-1])
  else:
    modified.append(line)
for i in range (9999999):
  randomint1 = (random.randint(0,498))
  randomint2 = (random.randint(0,498))
  item1 = modified[randomint1]
  item2 = modified[randomint2]
  print (item1, '\n'+ item2)
  answer = input('item 1 or 2')

this is what i have so far, i don't know how i'd go about linking the list of items to a list of scores

ideally i could read the items in ranking of top to bottom, possibly in a created txt file with the name of items listed next to score? i'm not too sure.

any help is appreciated, thanks

  • suggest rewording some of the question to better explain 1. what you're trying to do 2. what's not working and 3. what you've tried without me having to watch a whole video. Is this correct as the problem? - given a file with a list of items, you're trying to get input from the console to have a user pick between 2 randomly selected items. Each item has a score which starts at 0 and when selected, the score increases by 1 and the unselected item's score decreases by 1. – ccchoy Oct 21 '22 at 17:53

1 Answers1

0

Given a file where each line in the file is some str item label like:

Plate
Fork
Spoon

and you want to write a program that will read all of the items from the file and then continuously ask a user one or the other while keeping tally of those results in the form of a single score value per item which goes +1 or -1 depending on if it was chosen in each decision.

with open("items.txt", "r") as f:
    items = f.read().splitlines()  # removes the \n @ the end vs readlines() see https://stackoverflow.com/questions/12330522/how-to-read-a-file-without-newlines

item_scores = {item: 0 for item in items}  # could also use a `collections.defaultdict(int)`

while True:
    item_1, item_2 = randomly_select_two_items(items)
    raw_input = ""
    while raw_input not in {item_1, item_2}:
        raw_input = input(f"{item_1} or {item_2}? ")
    if raw_input == item_1:
        item_scores[item_1] += 1
        item_scores[item_2] -= 1
    else:
        item_scores[item_2] += 1
        item_scores[item_1] -= 1
    print(f"The highest scored item is: {max(item_scores, key=item_scores.get)}")

print("Good Bye")

For completeness sake, I left a stub above for randomly_select_two_items(items) - here's a possible implementation to consider vs what you have in your example:

def randomly_select_two_items(items: list):
    import random
    return random.sample(items, 2)
ccchoy
  • 704
  • 7
  • 16