Home > Back-end >  Visual Studio code is not giving output in terminal
Visual Studio code is not giving output in terminal

Time:09-02

so I Just installed Visual studio code.. And I Installed the Python Extensions And Installed Python Itself But Here is the problem :

This is my code. It Generates random number and then gives the user 3 tries to guess it :

import random

RandomNumber = random.randint(1,6)

tries = 3
Outofchoice = False

while Outofchoice :
    UserInput = (input("There is a Random number between 1 and 6! you have 3 tries : "))
    if UserInput!= RandomNumber:
        tries -= 1 
    if tries == 0:
        print('you lost!')
        outofchoice = True

If My code Has a problem it will show me a error of that problem in the terminal. but if it doesn't have a problem the terminal Will not show me Anything. I Would be happy to have the problem fixed

CodePudding user response:

You assign Outofchoice = False and the loop condition is while Outofchoice. Therefore your loop never executes. Consider this simple alteration that uses the number of tries as the loop condition. Also note that you need to cast the user input to an integer. Otherwise you are comparing a string to an int which will always be False.

import random

random_number = random.randint(1,6)

tries = 3
found = False

while tries > 0:
    user_input = int(input(f"There is a Random number between 1 and 6! you have {tries} tries : "))
    if user_input != random_number:
        print("Guess again")
        tries -= 1 
    else:
        found = True
        break

if found:
    print("You won!")
else:
    print("You lost!")
  • Related