Home > other >  Setting a list without writing it manually? (Python3)
Setting a list without writing it manually? (Python3)

Time:09-19

I'm having some issues with my python program. I need to generate a list of numbers from 1 to 100 for use inside of a variable.

import random
import time

tries = 0
sumcoin = 0

list = (1, 100)

def run():
        while True:
                global sumcoin
                global tries
                correct = (list)
                guessed = (list)
                if guessed == correct:
                        print (f"\033[0;32;40mCorrect Guess! It was {correct}, found after {tries} attempts.\033[0;37;40m.")
                        sumcoin=sumcoin 1
                        print (f"You guessed  {sumcoin} times Correctly!")
                        tries = 0
                        sumcoin=sumcoin 1
                time.sleep(1)
                tries=tries 1
run()

The code is intended to generate a number from 1 to 100, guess it, and if it guesses wrongly then it will restart the process I will make the variable random with random.choice() or randint. This is the snippet output:

user@user:~ $ python3 list.py 
Correct Guess! It was (1, 100), found after 0 attempts..
You guessed  1 times Correctly!
Correct Guess! It was (1, 100), found after 1 attempts..
You guessed  3 times Correctly!
Correct Guess! It was (1, 100), found after 1 attempts..
You guessed  5 times Correctly!
Correct Guess! It was (1, 100), found after 1 attempts..
You guessed  7 times Correctly!
Correct Guess! It was (1, 100), found after 1 attempts..
You guessed  9 times Correctly!
Correct Guess! It was (1, 100), found after 1 attempts..
You guessed  11 times Correctly!
Correct Guess! It was (1, 100), found after 1 attempts..

As you can see, it only guessed the exact variable "(1, 100)". I am trying to make a random number out of 100 each time a new number is guessed. I searched online for the issue, but could not find any reliable results that could help me. Any and all assistance would be really, really appreciated.

CodePudding user response:

First off, your Python isn't very pythonic. Update your IDE to use 4-space indents instead of 8. Also the use of global here seems more like habit from another language? not sure of its purpose here

Next, your issue is that you're comparing your variables correct and guessed to each other, when you have set them to the exact same thing. You're basically saying if "this" == "this" for every iteration. Outside of that, your list isn't a list, it's a tuple containing just 1 and 100. What you're really looking for (to answer your question) is:

list = range(1, 100)

You might want to use argparse to pass a guess to the script, and you should also set up your script with if __name__ == "__main__" and run it from the command line or through your IDE.

  • Related