0
chosenNumber = input ("Choose A Number: ")
if chosenNumber.isdigit():
    chosenNumber = int(chosenNumber)
else:
    while chosenNumber.isalpha():
        if chosenNumber == chosenNumber.isdigit():
            chosenNumber = int(chosenNumber)
            break
        input ("Please Choose A Number: ")

I tried to make it so if the input is not a number it will keep asking him to choose a number but the while loop just keeps going is there a fix for that?

  • 1
    `if chosenNumber == chosenNumber.isdigit():` should be `if chosenNumber.isdigit():` – Barmar Dec 13 '22 at 19:00
  • 1
    Why do you need that `if` statement at all? The `while` condition is all you need. – Barmar Dec 13 '22 at 19:01
  • 1
    Better solutions here: [Asking the user for input until they give a valid response](/q/23294658/4518341) – wjandrea Dec 13 '22 at 19:02
  • Barmar's right, although, why did you choose `chosenNumber.isalpha()`? That'll ignore entries that are, for example, a mix of numbers and letters. Why not `not chosenNumber.isdigit()`? – wjandrea Dec 13 '22 at 19:04

1 Answers1

2

Consider rewriting whole part like this (without if and double input):

chosen_number = ''
while not chosen_number.isdigit():
    chosen_number = input("Please Choose A Number: ")
chosen_number = int(chosen_number)

you don't change this variable in your code, so it always contains first input

svfat
  • 3,273
  • 1
  • 15
  • 34