Python program loop issue
while loop in python
for loop python
python while loop exercises
while loop in python - geeksforgeeks
for loop in python 3
python for loop increment
python for loop range
I am doing a Python program. My task is to ask the user to enter payroll information for a company. Set up a loop that continues to ask for information until the user enters "DONE". For each employee ask three questions:
- first and last name
- hours worked this week (only allow 1 - 60)
- hourly wage (only allow 6.00 - 20.00)
Here is my code:
while True: #initiate loop strNames = input("Enter the employee's first and last name:") strHours = input("Enter total number of hours worked this week:") strWage = input("Enter employee's hourly wage:") if strNames =="DONE": break #breaks loop else: if strHours < "1" or strHours > "60": print("Error") if strWage < "6" or strWage > "20": print("Error")
When I run the program and enter the information, it prints:
"Error Enter the employee's first and last name:"
Can anyone help me/guide me in the right direction?
You've compared strings, rather than numeric values. Since
"20" < "6", every string will fulfill one of your two conditions and print
- Review your class materials; learn to recognize the appropriate data type for an application, and work with that data type. In this case, you need to convert the input to
intand work with numeric values.
- Practice incremental programming: get a few lines working before you add more. In this example, you've gone two steps past the working code, which complicates your debugging.
loops in python, You've compared strings, rather than numeric values. Since "20" < "6" , every string will fulfill one of your two conditions and print Error . Review Caesar Cipher python program looping issue. @SuperBiasedMan gave you the working solution for keeping your code, but i would propose a simpler loop for your task
As stated in the previous post you cannot compare strings. What you can do is cast them as integers and then do the comparison.
The last requirement
Set up a loop that continues to ask for information until the user enters "DONE"
You are only doing this once in you application after the user answers all the questions. I am not sure if you intended for this? If you would prefer the user to have the ability to exit the application at any point you could re-factor your code as I did so below
questions = ["Enter the employee's first and last name:", "Enter total number of hours worked this week:", "Enter employee's hourly wage:"] var = ['strNames', 'strHours', 'strWage'] while True: #initiate loop x = 0 # declare a variable for incrementing list 'var' for q in questions: var[x] = input(q) if var[x].upper() =="DONE": # checking if the user entered 'DONE' break # breaks inner for loop x += 1 # increment list count by 1 try: if int(var) < 1 or int(var) > 60: print("\n>>> Hours worked this week error\n") if int(var) < 6 or int(var) > 20: print("\n>>> Hourly wage error\n") except: # catches any exception errors # if exception occurs, come in here and break out of loop break # break while loop
Comments added to the above code, so please read for clarification.
Python for Loop, In Python we have three types of loops for, while and do-while. Program to print squares of all numbers present in a list # List of integer numbers numbers = [1, Python supports to have an else statement associated with a loop statement. If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list. If the else statement is used with a while loop, the else statement is executed when the condition becomes false.
while True: #initiate loop strNames = input("Enter the employee's first and last name: ") if strNames =="DONE": break #breaks loop strHours = int(input("Enter total number of hours worked this week: ")) if strHours < 1 or strHours > 60: print("Error 1") break strWage = int(input("Enter employee's hourly wage: ")) if strWage < 6 or strWage > 20: print("Error 2") break if strNames =="DONE": break #breaks loop
Python while Loop, By "block of code", I mean, any code. For example, here's the Python code to print "hello world" : print("hello world"). And here's the script that repeats that "block Python conditional statements and loops [44 exercises with solution] [An editor is available at the bottom of the page to write and execute the scripts.1. Write a Python program to find those numbers which are divisible by 7 and multiple of 5, between 1500 and 2700 (both included).
Python program loop issue, (Python 3 uses the range function, which acts like xrange). When the loop condition of "for" or "while" statement fails then code part in "else" is executed. Using these exercises, you can practice various Python problems, questions, programs, and challenges. All exercises are tested on Python 3. Each Exercise has 10 Questions. The solution provided for every question. Practice each Exercise in Code Editor. These Python programming exercises are suitable for any Python developer.
Python for Loop explained with examples, Loops. Moebius ring as an infinite loop. General Structure of a Loop. Many algorithms make it necessary for a programming language to have a construction Python Exercises, Practice, Solution: Python is a widely used high-level, general-purpose, interpreted, dynamic programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or Java.
For-loop fundamentals, . This is a similar construct, taken from the link above. executes one leg, as desired, even though the predicate's already false at the start. Following Python section contains a wide collection of Python programming examples. The examples are categorized based on the topics including List, strings, dictionary, tuple, sets, and many more. Each program example contains multiple approaches to solve the problem
- I have stuck to your code as much as possible, but have changed the order of the statements. In this form the program works fine. Also, the loop will end if you input "DONE" in response to "Enter the employee's first and last name:"