My Python number guessing game

2024/10/14 15:23:17

I have been trying to make a number guessing game for Python and so far it has altogether gone quite well. But what keeps bugging me is that it resets the number on every guess so that it is different, but I want it to stay the same and then people can take a while guessing it.

import os
import timeprint("this is a random number generator")
number1 =int(input("please pick a NUMBER between 1 and 99"))
from random import randint
number2 = int(randint(1,99))
if number1 == number2:print(" congrats! you got it right")
elif number1 < number2:print ("too low try again")
elif number1 > number2:print ("too high try again")if number1 not in range(1,99):print("you idiot! Pick a number IN THE RANGE")while number1 != number2:print("this is a random number generator")number1 =int(input("please pick a NUMBER between 1 and 99"))from random import randintnumber2 = int(randint(1,99))if number1 == number2:print(" congrats! you got it right")elif number1 < number2:print ("too low try again")elif number1 > number2:print ("too high try again")if number1 not in range(1,99):print("you idiot! Pick a number IN THE RANGE")time.sleep(10)
os.system("exit")
Answer

Since you have number2 = int(randint(1,99)) inside your while cycle, then you create a new number each time. Put that line outside the while and the number will remain the same until someone guesses it

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

Related Q&A

Class that takes another class as argument, copies behavior

Id like to create a class in Python that takes a single argument in the constructor, another Python class. The instance of the Copy class should have all the attributes and methods of the original clas…

Simple python script to get a libreoffice base field and play on vlc

Ive banged my head for hours on this one, and I dont understand the LibreOffice macro api well enough to know how to make this work:1) This script works in python:#!/usr/bin/env python3 import subproce…

Print month using the month and day

I need to print month using the month and day. But I cannot seem to move the numbers after 1 to the next line using Python.# This program shows example of "November" as month and "Sunday…

Maya: Defer a script until after VRay is registered?

Im trying to delay a part of my pipeline tool (which runs during the startup of Maya) to run after VRay has been registered. Im currently delaying the initialization of the tool in a userSetup.py like…

Optimization on Python list comprehension

[getattr(x, contact_field_map[communication_type])for x in curr_role_group.contacts ifgetattr(x, contact_field_map[communication_type])]The above is my list comprehension. The initial function and the …

tensorflow Word2Vec error

I downloaded source code of word2vec in github below. https://github.com/tensorflow/models/blob/master/tutorials/embedding/word2vec.py I am using tensorflow on pycharm. Im using windows 10. I installed…

Mysterious characters at the end of E-Mail, received with socket in python

I am not sure if this is the right forum to ask, but I give it a try. A device is sending an E-Mail to my code in which I am trying to receive the email via a socket in python, and to decode the E-Mail…

Error when importingPython-vlc

I have installed python-vlc D:\Programing\Python\Python 3.6>python -m pip install python-vlc Requirement already satisfied: python-vlc in d:\programing\python\python 3.6\lib\site-packages\python_vlc…

How to use FEniCS in Jupyter Notebook or Spyder?

I have recently installed FEniCS using Docker with following commandTerminal> curl -s https://get.fenicsproject.org | bashEvery thing works well when I am in fenicsproject session. In this session w…

Error installing polyglot in python 3.5.2

I want to do sentiment analysis on urdu sentences. I searched a python package Polyglot having URDU POS tagger in it. But on installing, it prompts error;Any way out?