AnsweredAssumed Answered

Random number of a Random number

Question asked by jlm180006@utdallas.edu_UTDEPPS on Apr 25, 2020
Latest reply on Apr 26, 2020 by jlm180006@utdallas.edu_UTDEPPS

Ok, let me see if I can get this down for every one to understand...

I am doing a guessing game in python/PyQt5...

I am trying to put in a hint button. So I am trying to generate a random number of a random number, but I don't know what function to use.

so the random number is generated and if you hit the hint button I want the second random number to be within +/-  (N) of the initial random number

 

import random
import sys

from PyQt5.QtWidgets import *

from frmguess import Ui_frmguess

# Declare global variables to store random number and times guessed
times_guessed = 0
num_to_guess = random.randrange(1, 101)                             #Gives random number
random2 = random.randrange(5, 5, num_to_guess)                  ????what function to use????

# Create main window (inherits from the base QT widget)
class Mainwindow(QWidget):
     def __init__(self):
          """Initialization code for when mainwindow object is created"""
          super().__init__()
          # Call UI set up in QtDesignerpyuic
          self.ui = Ui_frmguess()
          self.ui.setupUi(self)
          # Connect guess button click signal to guess_click slot function
          self.ui.guessbutton.clicked.connect(self.guess_click)
          # Reset times guessed label
          self.ui.timesguesslabel.setText(str(times_guessed))
          # Connect hint button click signal to hint_button
          self.ui.hint_button.clicked.connect(self.hint_click)
          # Show the form
          self.show()
     def hint_click(self):
          """What to do when user clicks the hint button"""
          # Get access to random number generated
          global num_to_guess
          global random2
          # Tell user is plus or minus another random number
          hint, okPressed = QMessageBox.question(self, "Your hint is...",
          "Number is plus or minus five " + str(random2),
          QMessageBox.Ok)

 

 

Any leads would be great

Outcomes