Why python gave me Hot not really hot? - python

temp = 120
if temp > 85:
print "Hot"
elif temp > 100:
print "REALLY HOT!"
elif temp > 60:
print "Comfortable"
else:
print "Cold"

Python picks the first condition that is true, and the rest of the if..elif..else branches are skipped.
120 > 85 is true, so the first test passes and 'Hot' is printed. It doesn't matter that the second test also matches after that point.
Put the > 100 test first:
if temp > 100:
print "REALLY HOT!"
elif temp > 85:
print "Hot"
elif temp > 60:
print "Comfortable"
else:
print "Cold"
Alternatively, limit the tests to exclude the upper range:
if 100 >= temp > 85:
print "Hot"
elif temp > 100:
print "REALLY HOT!"
elif temp > 60:
print "Comfortable"
else:
print "Cold"

Related

How may I get the desired output in my code?

I was attempting to solve some python excercise. I came across a question and that has made me feel bored. Please will you resolve it?
A school has following rules for grading system:
Below 25 - F
25 to 45 - E
45 to 50 - D
50 to 60 - C
60 to 80 - B
Above 80 - A
Ask user to enter marks and print the corresponding grade.
print("My mark is ")
a = input()
if '25 > a':
print('F')
elif a < 25 and a > 45:
print('E')
elif 45 <= a and a >|= 50:
print('D')
elif 50 <= a and a >= 60:
print('C')
elif 60 <= a and a >= 80:
print('B')
else:
print('A')
My expected result was different grades for different numbers but instead got only F for every input I do...
print("My mark is ")
a = int(input())
if a < 25:
print('F')
elif a >= 25 and a < 45:
print('E')
elif a >= 45 and a < 50:
print('D')
elif a >= 50 and a < 60:
print('C')
elif a >= 60 and a < 80:
print('B')
else:
print('A')
first of all, you should cast input to int. then, you just compare it and "a" should be the first in comparing like a > 25, not 25 < a
Multiple issues..
Remove the outside quotes of your if statements as those are strings.
elif '45 <= a and a => 50': the order must be >=
And you must compare with an int so you need to do int(input()) or another variation of converting to int type.
a = int(input('What is the grade?'))
print("My mark is ")
if 25 > a:
print('F')
elif a <= 25 and a > 45:
print('E')
elif 45 <= a and a >= 50:
print('D')
elif 50 <= a and a >= 60:
print('C')
elif 60 <= a and a >= 80:
print('B')
else:
print('A')
your code needs a little clean up. Dont use quotation around the condition which is supposed to be Boolean
a = input("My mark is ")
a=int(a)
if (25 > a):
print('F')
elif a <= 25 and a > 45:
print('E')
elif 45 <= a and a >= 50:
print('D')
elif 50 <= a and a >= 60:
print('C')
elif 60 <= a and a >= 80:
print('B')
else:
print('A')

How to get the right output from this python code?

I have a python code, it cannot get the correct output. the code is shown as follows:
score = int(input("Please input a score: "))
grade = ""
if score < 60:
grade = "failed"
elif score < 80: # between 60 and 80
grade = "pass"
elif score < 90:
grade = "good"
else:
grade = "excellent"
print("score is (0), level is (1)".format(score,grade))
Can anyone tell me where is the problem? Thank you so much!
You should change your if statement to:
if score <= 60:
grade = "failed"
elif score <= 80 and score > 60: # between 60 and 80
grade = "pass"
elif score <= 90 and score > 80:
grade = "good"
elif score > 90: #Assuming there is no max score since before, you left the last statement as an else.
grade = "excellent"
else: #Not necessary but always nice to include.
print("Not a valid score, please try again.")
And, as #loocid commented, change print("score is (0), level is (1)".format(score,grade)) to print("score is {0}, level is {1}".format(score,grade))
Though I prefer
print("score is " + str(score) + ", level is " + str(grade))

Jython range issue

I am writing this program to take a list of input classes along with scores to with them. The goal is to print the class input along with the letter grade associated with the number score input.
However when more than 1 class is put in, it gives me an error saying the sequence index is out of range. Also no matter what score I input it always prints "A" next to the class.
course_list = []
score_list = []
grade_list = []
while True:
course = requestString("Enter a class name or 'q' to quit")
if course == 'q':
break
else:
course_list.append(course)
score = requestString("Enter the class score")
score_list.append(score)
if score >= 90:
grade_list.append(" A")
elif score >= 80:
grade_list.append(" B")
elif score >= 70:
grade_list.append(" C")
elif score >= 60:
grade_list.append(" D")
else:
grade_list.append(" F")
print "-=Class Scores=-"
final_list = [course_list, grade_list]
for i in range(len(course_list)):
final = ''
for j in range(len(final_list)):
final += (final_list[j][i])
print final
This has nothing to do with Jython.
All you have to do is to indent the whole if score [...]-Block to the same level as the score_list.append(score).
The problem with your code is that this block was outside the while-loop, so you extend the course_list and the score_list until the input is q, but only after this is done you take the last score and extend the grade_list once.
So the grade_list always has the length of 1 (or 0, if no course was given) and the indexing final_list[j][i] fails, because it expects the length of the course_list to be equal to the length of the grade_list.
course_list = []
score_list = []
grade_list = []
while True:
course = requestString("Enter a class name or 'q' to quit")
if course == 'q':
break
else:
course_list.append(course)
score = requestString("Enter the class score")
score_list.append(score)
# this has to happen inside the while-loop:
if score >= 90:
grade_list.append(" A")
elif score >= 80:
grade_list.append(" B")
elif score >= 70:
grade_list.append(" C")
elif score >= 60:
grade_list.append(" D")
else:
grade_list.append(" F")
print "-=Class Scores=-"
final_list = [course_list, grade_list]
for i in range(len(course_list)):
final = ''
for j in range(len(final_list)):
final += (final_list[j][i])
print final

Variable not storing the correct value in Python

So I have written a program in python to try and convert a number typed into the letter version of that number. Ex: 323 -> three hundred twenty-three
The problem I am having is that the one of the variables is not displaying the correct value when the process is complete and the output is displayed. The desired output would look something like this:
Enter a number under 999: 323
323 -> three hundred twenty-three
but instead looks like this:
Enter a number under 999: 323
23 -> three hundred twenty-three
If anyone can help me figure this out, I would greatly appreciate it. Here is my code:
from __future__ import print_function, division
import sys
input = raw_input
n = int(input("Enter a number under 999: "))
if n >= 999:
print("Well, you didn't follow directions.")
sys.exit(1)
word = ""
hundred = n//100
if hundred == 1:
word += "one hundred"
elif hundred == 2:
word += "two hundred"
elif hundred == 3:
word += "three hundred"
elif hundred == 4:
word += "four hundred"
elif hundred == 5:
word += "five hundred"
elif hundred == 6:
word += "six hundred"
elif hundred == 7:
word += "seven hundred"
elif hundred == 8:
word += "eight hundred"
elif hundred == 9:
word += "nine hundred"
if hundred > 0:
word += " "
n = n%100
if n == 10:
word += ' ten'
elif n == 11:
word += ' eleven'
elif n == 12:
word += ' twelve'
elif n == 13:
word += ' thirteen'
elif n == 14:
word += ' fourteen'
elif n == 15:
word += ' fifteen'
elif n == 16:
word += ' sixteen'
elif n == 17:
word += ' seventeen'
elif n == 18:
word += ' eighteen'
elif n == 19:
word += ' nineteen'
else:
ones = n%10
tens = n//10
if tens == 2:
word += "twenty"
elif tens == 3:
word += "thirty"
elif tens == 4:
word += "fourty"
elif tens == 5:
word += "fifty"
elif tens == 6:
word += "sixty"
elif tens == 7:
word += "seventy"
elif tens == 8:
word += "eighty"
elif tens == 9:
word += "ninety"
if tens > 0 and ones > 0:
word += '-'
if ones == 1:
word += 'one'
elif ones == 2:
word += 'two'
elif ones == 3:
word += 'three'
elif ones == 4:
word += 'four'
elif ones == 5:
word += 'five'
elif ones == 6:
word += 'six'
elif ones == 7:
word += 'seven'
elif ones == 8:
word += 'eight'
elif ones == 9:
word += 'nine'
print("{} -> {}".format(n, word))
Oh and btw, My class is learning python 3 while using a python 2 interpreter so that's why the code has some weird aspects to it.
since
n = n%100
mutates "n", you need to "save" the value of "n" first. the most simple method would be:
after declaring
n = int(input("Enter a number under 999: "))
set another variable to n
n = int(input("Enter a number under 999: "))
num = n
then replace
print("{} -> {}".format(n, word))
with
print("{} -> {}".format(num, word))
Change your code to
n = int(input("Enter a number under 999: "))
user_number = n
(...)
print("{} -> {}".format(user_number, word))
Every time you do operations on n you modify it:
n = n%100
So just save it in another variable before modifying it (user_number) and print it at the end.

String in Python

I'm a french newbie in Python and I would like to code a program which warns us when the time (string "Day, Hours, Minutes, Seconds") is wrong (for example, 83 seconds).
I did this program:
t=input("Put day,hours,minutes,seconds: ")
t="j,h,m,s"
if int(t[6])<=0 or int(t[6])>=59:
print("Seconds wrong")
if int(t[4])<=0 or int(t[4])>=59:
print("Minutes wrong")
if int(t[2])<=0 or int(t[2])>=24:
print("Hours wrong")
if int(t[0])<=0 or int(t[0])>=31:
print("days wrong")
else:
print("OK")
But I've this error:
if t[6]<=0 or t[6]>=59:
TypeError: unorderable types: str() <= int()
So I put "int" everywhere (like "int(t[X])<=0")
but then I've this error:
if int(t[6])<=0 or int(t[6])>=59:
ValueError: invalid literal for int() with base 10: 's'
There are no digits in this string:
t="j,h,m,s"
so any attempt to do int(t[anything]) is going to fail. You can't convert a string to an integer unless the string contains a string representation of an actual integer, such as t = "1234".
Moreover, even if you had something like t = "31,11,22,45", then int(t[6]) would not give you the number of seconds, because the representation of seconds would be at indices 9 and 10. You'd need t = int(t[9:11]) in that case.
Something like this is what you're looking for:
#!/usr/bin/python
t = "31,11,22,45"
(day, hour, min, sec) = [int(elem) for elem in t.split(',')]
if not 0 <= sec <= 59:
print("Seconds wrong")
elif not 0 <= min <= 59:
print("Minutes wrong")
elif not 0 <= hour <= 23:
print("Hours wrong")
elif not 1 <= day <= 31:
print("days wrong")
else:
print("OK")
Note you either need to change all but your first if to elif, otherwise it'll print "OK" if day is correct but everything else is wrong, or you'll need to keep some kind of separate variable to store if anything is wrong, and check that at the end, such as in the following:
#!/usr/bin/python
t = "31,11,22,45"
(day, hour, min, sec) = [int(elem) for elem in t.split(',')]
time_ok = True
if not 0 <= sec <= 59:
print("Seconds wrong")
time_ok = False
if not 0 <= min <= 59:
print("Minutes wrong")
time_ok = False
if not 0 <= hour <= 23:
print("Hours wrong")
time_ok = False
if not 1 <= day <= 31:
print("Days wrong")
time_ok = False
if time_ok:
print("Time is OK")
Nice effort, but be careful with the second line:
t = "j,h,m,s"
This overwrites the user input, and assigning "j,h,m,s" to t instead. Remove it!
Also, you can't compare it using or. Just check if int(t[6]) > 59 is sufficient.
if int(t[6]) > 59:
print("seconds wrong")
A better way to get the comma-separated numbers is to use string.split() method.
t = t.split(',') #split the string in the commas
Now, you don't have to worry about the comma counts. The day would be t[0], the hours would be t[1], and so on.
Ah, also one more thing. You don't have to use if statements repeatedly there. Use it on the first time, and change the next ones with elif statements.
Full fixed code:
t = input("Put day,hours,minutes,seconds: ")
if int(t[6])>59:
print("Seconds wrong")
elif int(t[4]) < 0 or int(t[4]) > 59:
print("Minutes wrong")
elif int(t[2]) < 0 or int(t[2]) > 24:
print("Hours wrong")
elif int(t[0]) < 0 or int(t[0]) > 31:
print("days wrong")
else:
print("OK")
To be honest, as Paul mentioned, this still won't work if any input is more than one digit.
You'll have to use string.split() to achieve this.
t = input("Put day,hours,minutes,seconds: ").split(",")
if int(t[3])>59:
print("Seconds wrong")
elif int(t[2]) < 0 or int(t[2]) > 59:
print("Minutes wrong")
elif int(t[1]) < 0 or int(t[1]) > 24:
print("Hours wrong")
elif int(t[0]) < 0 or int(t[0]) > 31:
print("days wrong")
else:
print("OK")

Categories

Resources