How to divide variable from input by two

Question:

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=' ')
Asked By: Cruton246

||

Answers:

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 calling keyboard.is_pressed: what you are meant to do here is to check whether the user has entered K (if QA == "K") or, if you just want to continue as soon as K is pressed, use keyboard.wait('k'). I would recommend sticking to input 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 pressing Shift+k (as far as I can tell, not the case with the keyboard package).
  • input('I"m thinking of does not return anything. You probably want print 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
Answered By: Lodinn

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.

Answered By: Subxmi
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.