While Loop Guessing Number Game - Python

2024/7/6 22:42:39

I'm trying to make a 'guess the number between 1-10' game but the while loops seems to keep running. I want to program to let the user guess a number then display if its too high or low etc then start again automatically (loop) to allow the user to pick again. This code makes it run forever though. Can you guys help me?

import randomdef numberGuess():printNow("I'm thinking of a number between 1 and 10")guess = 0 # give guess a starting valuerandNum = random.randrange(1,11) # this line generates a random numberguess = int(input("Try to guess the number:")) # ask user for a numberprint randNum while guess != randNum:if (guess == randNum): print "You got it!"if (guess > randNum):print "Wrong! You guessed too high"if (guess < randNum):print "Wrong! You guessed too low"
Answer

You forgot to guess inside the loop

  while guess != randNum:guess = int(input("Try to guess the number:"))if (guess > randNum):print "Wrong! You guessed too high"if (guess < randNum):print "Wrong! You guessed too low"print "You got it!"
https://en.xdnf.cn/q/120701.html

Related Q&A

Calculating distance between word/document vectors from a nested dictionary

I have a nested dictionary as such:myDict = {a: {1:2, 2:163, 3:12, 4:67, 5:84}, about: {1:27, 2:45, 3:21, 4:10, 5:15}, apple: {1:0, 2: 5, 3:0, 4:10, 5:0}, anticipate: {1:1, 2:5, 3:0, 4:8, 5:7}, an: {1:…

Is there anything wrong with the Python code itself? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python Iterate Over String

So Ive got a string e.g "AABBCCCASSDSFGDFGHDGHRTFBFIDHFDUFGHSIFUGEGFGNODN".I want to be able to loop over 16 characters starting and print it. Then move up 1 letter, loop over 16 characters a…

what does with open do in this situation [duplicate]

This question already has answers here:What is the Python "with" statement used for?(3 answers)Closed 7 years ago.sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN D…

How to perform HTTP GET operation in Python? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Python Error TypeError: cannot concatenate str and float objects [duplicate]

This question already has answers here:Making a string out of a string and an integer in Python [duplicate](5 answers)Closed 7 years ago.I am new with Python programming. I keep getting the below error…

Countif function in python

enter image description here In excel file i can do countif funtion like attached picture but How can i do this countif function in Python Pandas,please help me by providing the code

How do i implement these algorithms below

Alogrithm 1:Get a list of numbers L1, L2, L3....LN as argumentAssume L1 is the largest, Largest = L1Take next number Li from the list and do the followingIf Largest is less than LiLargest = LiIf Li is …

How to run a shell script once a day?

I am trying to run this particular shell script only one time, daily. Heres my code for runLucene.py:#!/usr/bin/env pythonimport os from extras.download_datos_desambiguar import news_Lucenex=datetime.t…

How to fix Error: Please select a valid Python interpreter in Pycharm?

Error:Error: Please select a valid Python interpreterScreenshot:How to fix this?