Input() function not accepting prompts

Question:
I am trying to make a number guessing game but every time I try and use the input() method some kind of error shows up. I have linked a couple of the examples of where it happens and also the whole program below.

Repl link:
https://replit.com/@randomcat862/Number-Guessing-Game

input = input("Please choose a difficulty. (easy, normal, hard, extra hard): ")
guess = int(input("Guess a number between 1 and", str(self.diff) + ": "))
import os, random

class game:
  def __init__(self, diffNum):
    if diffNum == 1:
      self.diff = 10
    if diffNum == 2:
      self.diff = 20
    if diffNum == 3:
      self.diff = 100
    else:
      self.diff = 1000
  def run(self):
    num = random.randint(1, self.diff)
    playing = True

    while playing:
      guess = int(input("Guess a number between 1 and", str(self.diff) + ": "))
      if guess == num:
        print("You guessed it!")
        os.system("clear")
        playing = False
      elif guess < num:
        print("Too low!")
      else:
        print("Too high!")

playing = True
while playing:
  input = input("Please choose a difficulty. (easy, normal, hard, extra hard): ")
  if input == "easy":
    difficulty = 1
  elif input == "normal":
    difficulty = 2
  elif input == "hard":
    difficulty = 3
  elif input == "extra hard":
    difficulty = 4
  else:
    print("Invalid difficulty")
    exit()
  ngg = game(difficulty)
  ngg.run()
  print("Press enter to play again.")
  if input() == "":
   os.system("clear")
  else:
    playing = False

os.system("clear")
print("Thanks for playing!")

You are passing multiple arguments to input(). This will give an error.

#                 \/-------arg 1--------------\/  \/------arg 2------\/
guess = int(input("Guess a number between 1 and", str(self.diff) + ": "))

To fix this, concatenate your strings into a single string, before passing it to input.

guess = int(input("Guess a number between 1 and " + str(self.diff) + ": "))

Even better, use formatted string literals to place values directly in strings.

guess = int(input(f"Guess a number between 1 and {self.diff}: "))

Also, in your other code, you are overwriting (shadowing) the input() builtin function with your own variable, named the same thing.
You should avoid having variable names that are the same as any builtin function.

input = int(input("enter '3'"))
print(input())  # TypeError: int can't be called
#       /\ not builtin input function anymore, it's now the variable that you assigned

Correct:

inp = input('prompt...')
5 Likes

I rried doing that and changing the comma to a pus sign, but that didn’t work so I tried using a variable instead, that didn’t work either. I eventually just settled on printing the prompt in a print() statement but it still didn’t work with nothing in it at all.

I missed some of your reply, I’ll try changing some of my variable namss too of I can.

Nevermind I got it to work. Thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.