'How do I break out loops with a keyboard input in Python
All this below is the context for the main program, could mostly be ignored if you change it around
from random import randint
from time import sleep
import cursor
numbers = 0
random = 0
mini = 0
maxi = 0
nums = list()
cursor.show()
while True:
try:
miniRange = int(input("Enter the Lower Range: "))
maxiRange = int(input("Enter the Maxium Range: "))
if miniRange > maxiRange:
print("Minium number is higher than Maxium number!")
elif miniRange < maxiRange and maxiRange > miniRange:
break
except:
print("Error! Not a number!")
while True:
try:
finder = int(input(f"Enter the number you want to find between {miniRange} and {maxiRange}: "))
if finder < miniRange:
print(f"Not valid! Lower than {miniRange}!")
elif finder > maxiRange:
print(f"Not valid! Higher than {maxiRange}!")
elif finder >= miniRange and finder <= maxiRange:
print("--------------------------------------")
break
except:
print("Error! Not a number!")
cursor.hide()
This is the loop I want to break out of, I've tried while x != "" and I've tried importing keyboard but I couldn't find a solution
while True:
try:
random = randint(miniRange, maxiRange)
numbers += 1
nums.append(random)
print(random)
if random == finder:
mini = min(nums)
maxi = max(nums)
print(f"\nIt took {numbers} Tries to find {finder}!")
print(f"\nThe lowest number found is! {mini}")
print(f"\nThe highest number found is! {maxi}")
print("--------------------------------------\n")
numbers = 0
nums.clear()
sleep(5)
except:
print("You messed up in a great capacity")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
