-2
import random as r

print(
"""
        Welcome to the Custom Counter!
    
    Type in your starting value and your
    ending value. Then, enter the amount
    by which to count.
""")

x = True

start = int(input("Starting number: "))
while x != False or start != "":
    fin = int(input("Ending number: "))
    amount = int(input("Count by: "))
    if amount > 0 or amount < 0:
        x = False
        
result = r.randrange(start, fin, amount)    
print(result)

input("Hit enter to exit.")

I made the while loop in hopes to exit the program when you clicked enter. Unfortunately, this will not let me exit the while loop no matter what I do. I am a noob at python. What is wrong with my condition?

Thank you!

petezurich
  • 9,280
  • 9
  • 43
  • 57
  • 4
    You never modify `start`. What's more, since `start` is an `int`, it's always going to be unequal to `""`. Does the second part of your `while` condition really help matters here? – Dominik Stańczak Sep 29 '22 at 04:49
  • `if amount > 0 or amount < 0:` we typically write this as `if amount == 0:`, which will always give you `r.randrange(start, fin, 0)` which will not work – Thomas Weller Sep 29 '22 at 05:01
  • see here https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response Asking the user for input until they give a valid response – pippo1980 Sep 29 '22 at 08:22
  • and here https://stackoverflow.com/questions/23326099/how-can-i-limit-the-user-input-to-only-integers-in-python How can I limit the user input to only integers in Python too see helper answer from Ahsan Roy – pippo1980 Sep 29 '22 at 08:25
  • use a big while True loop and within it a try: all inputs then break ; except continue – pippo1980 Sep 29 '22 at 08:30

2 Answers2

0

Welcome to stackoverflow and welcome to Python!

Try to use and instead of or.

In your line:

while x != False or start != "":

You say to python, you want to run until 1st or 2nd condition is true. Since the 2nd condition stays always true, while will run forever.

Even more simple:

while x and start:

Hope you will enjoy Python as I do!

rundekugel
  • 1,118
  • 1
  • 10
  • 23
0

my code :

import random as r

print(
"""
        Welcome to the Custom Counter!
    
    Type in your starting value and your
    ending value. Then, enter the amount
    by which to count.
""")


while True:
    
    try:
        start = int(input("Starting number: "))
        fin = int(input("Ending number: "))
        amount = int(input("Count by: "))
    
    except ValueError as verr:
        
        print('error : ', verr,'\n restarting ....')
        
    else:
        
        break
        
   
result = r.randrange(start, fin, amount)    
print('result : ' ,result)

input("Hit enter to exit.")

this one I believe takes into account inputting of non ints and restart program from scratch when encountering such kind of inputs.

See The try statement specifies exception handlers and/or cleanup code for a group of statements

PS your code doesnt handle invalid values (i.e. fin < start)

pippo1980
  • 2,181
  • 3
  • 14
  • 30