MarKK
MarKK

Reputation: 13

How do i convert the input into an int?

I tried using int() to convert the input into int, but I just get a TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'. How do i solve this?

import random

number = random.randint(1, 50)
lives = 10

guess = int(print("Enter a number between 1 and 50: "))


while guess != number and lives != 0:
    if guess > number:
        guess = int(print("Your guess is higher than the secret number. Enter another guess: "))
        lives -= 1
    else:
        guess = int(print("Your guess is lower than the secret number. Enter another guess: "))
        lives -= 1

if guess == number:
    print("You win!")
else:
    print("You lose!")

Upvotes: 0

Views: 129

Answers (4)

Alien
Alien

Reputation: 1

You are attempting to cast the output of print() to int when, as the error message suggests, print() outputs None.

If you replace the line 6 "print" with "input" your code will work (as seems expected) with python 3.

Upvotes: 0

Hasano
Hasano

Reputation: 31

guess = int(print("Enter a number between 1 and 50: ")) is wrong. it should be

guess = int(input('enter a number between 1 and 50: '))

Upvotes: 0

Msv
Msv

Reputation: 1371

guess = int(input("Enter a number between 1 and 50: "))

By default input takes everything as a string. And we convert the type afterwards.

Upvotes: 0

Chris Happy
Chris Happy

Reputation: 7295

Doesn't print() just show some text?

Try using:

guess = int(input("Enter a number between 1 and 50: "))

https://www.geeksforgeeks.org/taking-input-in-python/

Upvotes: 1

Related Questions