Ok so if I wanted to do a simple while loop with a user inputting a number and a function doing some calculations, would it be possible to have it so that the user can type in 'quit' instead of a float if they want to quit? Thanks in advance.

Yes. You just check the lowercase input with 'quit'. Even easier is to quit by '' empty line as it acts as False in if statement. How ever it is easy to give aksidentaly.

For example

# -*- coding: utf-8 -*-

def check(number):
    if number % 2 == 0:
       return True
    else:
         return False

while True:
      try:
          number = int(raw_input("> Number "))
      except ValueError:
             print "> ERROR!"
             
      if check(number) == True:
         break
      else:
           continue

For example

# -*- coding: utf-8 -*-

def check(number):
    if number % 2 == 0:
       return True
    else:
         return False

while True:
      try:
          number = int(raw_input("> Number "))
      except ValueError:
             print "> ERROR!"
             
      if check(number) == True:
         break
      else:
           continue
# -*- coding: utf-8 -*-
from __future__ import print_function

def isodd(number): return number % 2

number=''
print("Give 'quit' to finish program.")
while number.lower() != 'quit':
    if number: print('Wrong input, try again')
    try:
        number = raw_input("Integer to check = ")
        if isodd(int(number)):
            print('You gave odd number!')
        else:
            print('You gave even number!')
        number=''
    except ValueError:
        continue

print('Bye, bye')

Kur3k that code will break because your exception handling is wrong.
The code dos not what what is ask for.
It check if an integer for odd/even and has no 'quit' check out.
The code example should like this to make sense.

def check(number):
    if number % 2 == 0:
        return True
    else:
        return False

while True:
      try:
        number = int(raw_input("> Number "))
        if check(number) == True:
            print 'Number did pass it is a even number'
            break
      except ValueError:
        print "Wrong input only numbers,try again"

More like this is what ask for.

my_numbers = []
print 'Enter numbers | calculate <c> quit <q>'
while True:
    try:        
        n = raw_input('Enter a number: ')
        if n.lower() == 'q':
            break        
        elif n.lower() == 'c':
            print sum(my_numbers)
            break
        my_numbers.append(float(n))    
    except ValueError:        
        print 'Please only numbers,try again'

Edited 6 Years Ago by snippsat: n/a

This question has already been answered. Start a new discussion instead.