On line 7 and 14 I cant figure out how to divide the variable.
import keyboard
import random
def main(Number, Start):
Number = random.randrange(1,100)
Start = False
QA = input('Press "K" key to begin')
if keyboard.is_pressed('K'):
Start = True
input('I"m thinking of a random number and I want that number divisible by two')
print(Number)
input('Please divide this by two. *IF IT IS NOT POSSIBLE RESTART GAME*\n')
if QA == int(Number) / 2:
print('.')
else:
print('.')
main(Number=' ' ,Start=' ')
CodePudding user response:
What you probably want:
- Pick a random number
- Make user divide this number by two (?)
- Do something based on whether the guess is correct
What is wrong with your code:
- You are not picking a number divisible by two. The easiest way to ensure that your number is, indeed, divisible by two, is by picking a random number and then multiplying it by two:
my_number = 2 * random.randrange(1, 50)
. Note the change in the range. Also note that the upper limit is not inclusive, which may be not what your meant here. A typical check for divisibility by N is using a modulo operator:my_number % N == 0
. If you want users to actually handle odd numbers differently, you would need to write a separate branch for that. input
returns a string. In your case,QA = input('Press "K" key to begin')
returns"K"
IF user has actually done that or random gibberish otherwise. Then you are checking a completely unrelated state by callingkeyboard.is_pressed
: what you are meant to do here is to check whether the user has enteredK
(if QA == "K"
) or, if you just want to continue as soon asK
is pressed, usekeyboard.wait('k')
. I would recommend sticking toinput
for now though. Note that lowercase/uppercase letters are not interchangeable in all cases and you probably do not want users to be forced into pressingShift k
(as far as I can tell, not the case with thekeyboard
package).input('I"m thinking of
does not return anything. You probably wantprint
there, possibly with f-strings to print that prompt along with your random number.input('Please divide this by two.
does not return anything, either. And you definitely want to store that somewhere or at least immediately evaluate against your expected result.- There is no logic to handle the results any differently.
- Your function does not really need any arguments as it is written.
Start
is not doing anything, either. - Variable naming goes against most of the conventions I've seen. It is not a big problem now, but it will become one should you need help with longer and more complex code.
Amended version:
import random
import keyboard
def my_guessing_game():
my_number = random.randrange(1, 50) * 2
# game_started = False
print('Press "K" to begin')
keyboard.wait('k')
# game_started = True
print(f"I'm thinking of a number and I want you to divide that number by two. My number is {my_number}")
user_guess = input('Please divide it by two: ')
if int(user_guess) == my_number / 2:
# handle a correct guess here
print('Correct!')
pass
else:
# handle an incorrect guess here
pass
CodePudding user response:
Alternatively, you can use the modulo operator % to test whether Number is divisible by 2:
if Number % 2 == 0: print('.') else: print('.')
This will check whether the remainder of Number divided by 2 is equal to 0, which indicates that Number is divisible by 2.