using while loop in which i want to enter stop to stop the loop - loops

while True:
marks = int(input("Enter the marks: "))
print(marks)
if marks <= 40 :
print("Grade F")
elif marks <= 50 :
print("Grade E")
elif marks <= 70 :
print("Grade D")
elif marks <= 80 :
print("Grade C")
elif marks <= 90 :
print("Grade B")
elif marks <= 100 :
print("Grade A")
if marks == str.stop:
break
While trying to execute it shows me the below mentioned error:
invalid literal for int() with base 10: 'stop'

This error because you are comparing an integer value marks to a string value str.stop
so you need to make condition if the input is equal to "stop" the loop will be stopped using break
This is the code for your question
while True:
marks = input("Enter the marks (or enter 'stop' to stop the loop): ")
if marks == "stop":
break
marks = int(marks)
print(marks)
if marks <= 40:
print("Grade F")
elif marks <= 50:
print("Grade E")
elif marks <= 70:
print("Grade D")
elif marks <= 80:
print("Grade C")
elif marks <= 90:
print("Grade B")
elif marks <= 100:
print("Grade A")
Output
Enter the marks (or enter 'stop' to stop the loop): 100
100
Grade A
Enter the marks (or enter 'stop' to stop the loop): 50
50
Grade E
Enter the marks (or enter 'stop' to stop the loop): 80
80
Grade C
Enter the marks (or enter 'stop' to stop the loop): stop

Related

Extracting Integers from an Array and Creating a List of Strings

I'm basically just trying to get the number value into a letter grade but I jut can't think how to do it, really appreciate the help
#turned datadrame column of reading scores into an array
ReadingScoreArray = StudentExamsIndexed[['reading score']].to_numpy()
#my attempt at trying to iterate through the values to get A B C D F
for scores in ReadingScoreArray:
if 90 <= scores == 100:
scores = 'A'
elif 80 <= scores <=89:
scores = 'B'
elif 70 <= scores <= 79:
scores = 'C'
elif 60 <= scores <= 69:
scores = 'D'
else:
scores = 'F'
print(scores)

How do I repeat a for loop only for n times?

I need to create a program that asks you for 'a' and 'b' and gives you only the first 10 even numbers between 'a' and 'b, skipping 8 (in case there is one).
So far I have accomplished to print the even number between 'a' and 'b' skipping 8 but I have no idea how to limit this loop to iterate only 10 times.
Here is the code:
def pares(x,y):
for num in range(x,y):
if num%2==0:
if num == 8:
continue
print(num, end = ' ')
def main():
a = int(input('Dame un entero A: '))
b = int(input('Dame un entero B: '))
b += 1
pares(a,b)
main()
Add a counter and terminate the loop when you reach 10.
counter = 0
for num in range(x,y):
if counter == 10:
break
else:
if num%2==0:
if num == 8:
continue
print(num, end = ' ')
counter+= 1

Understanding control structure of nested loops in Python

All of the options below produce the same output, but I'm not quite understanding why. Is anyone able to explain why multiple values are printed for j on each line? I would think it would print either 0 every time when it is set equal to 0 or print 1, 2, 3, 4 instead.
Option 1:
for i in range(1, 6):
j = 0
while j < i:
print(j, end = " ")
j += 1
print("")
Option 2:
for i in range(1, 6):
for j in range(0, i):
print(j, end = " ")
print("")
Option 3:
i = 1
while i < 6:
j = 0
while j < i:
print(j, end = " ")
j += 1
i += 1
print("")
Output:
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
It is because of the inner while/for loop that one or more digits are printed on a single line.
As the value of i increments in the outer loop, the number of nested iterations increase with increasing value of i.
The digits are printed on the same line in inner loop due to end=" " argument to the first print statement and the next sequence appears on the new line because the second print statement in the outer iteration does not contain any such argument.
In order to gain better understanding, make following changes to your code, one by one and test run to see the effects:
Replace i in the inner loop with some constant value
Replace the space in end = " " with something else, e.g. end = "x"

python beginner: reading a file and adding its integers

so i'm trying to pull numbers from a file numbers.txt and add them together. The program can currently pull the numbers one at a time and print them spaced out on one line. I now need it to total all of the values. The numbers in the file are: 9 19 15 17 5 and 17. The total should be 82 but it will only add the two number 17's and output 34.
def main():
numfile = open('numbers.txt', 'r')
total = 0
for line in numfile:
line = line.rstrip('\n')
print (line, end=' ')
total = int(line)
total += total
print ("\nEnd of file")
print (total)
numfile.close()
main()
When You execute code total = int(line) - Main Error - resets the value of total
In [11]: numfile = open('numbers.txt', 'r')
In [12]: total = 0
In [13]: for line in numfile:
....: line = line.strip()
....: print line,
....: total += int(line)
....:
9 19 15 17 5 17
In [14]: total
Out[14]: 82

While loop with users input

I am trying to ask the user 2 questions. They are supposed to answer numbers greater than 0, but if they answer something like 1,0 or maybe 0,1 that is still a valid answer. If they answer both questions with 0's then the program is to terminate. Where am I going wrong here because I have been stuck on this for over an hour.
a = 1
b = 1
while a > 0 and b > 0:
a = float(input("Enter A: "))
b = float(input("Enter B: "))
if a > 0 and b > 0:
#calculate equations here
#calculate equations here
#print responses here
#print responses here
elif a == 0 and b == 0:
print("Good bye!")
If I understand correctly, I think what you are looking for is something like
a = 1
b = 1
while a > 0 or b > 0:
a = float(input("Enter A: "))
b = float(input("Enter B: "))
if a == 0 and b == 0:
print("Good bye!")
else:
#calculate equations here
#calculate equations here
#print responses here
#print responses here

Resources