This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
Learning python and currently learning the bisection method of solving problem. I'm writing code that should take in a users guess from 0 to 100 and attempt to find that guess using bisection. Here's the code:
answer = raw_input('Please think of a number between 0 and 100')
#I've been using 80 as my test case
low = 0
high = 100
guess = (low+high)/2
while guess != answer:
if guess < answer:
low = guess
else:
high = guess
guess = (low+high)/2
What I've realized is that when my guess < answer is false the else block doesn't execute, so my high number never changes. Why is this happening? Am I overlooking something here?
You need to convert user input to integer (raw_input() returns a string):
answer = int(raw_input(...))
Comparison fails because you are later comparing an integer to a string (which works in Python2, but would not work in Python3):
>>> 10 < "50"
True
>>> 75 < "50"
True
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
The code below is giving me a return of 25 with inputs of 3 & 4. Obviously it should be 7. This is a problem for school and I can't edit the first 3 lines or the last one. What am I missing here?
total_owls = 0
num_owls_A = input()
num_owls_B = input()
num_owls_A = int(input())
num_owls_B = int(input())
total_owls = (num_owls_A + num_owls_B)
print('Number of owls:', total_owls)
input() returns input value as a string. So, you are basically concatenating strings not integers.
If you want to add them as numbers you need to convert them to numbers first like below
num_owls_A = int(input())
num_owls_B = int(input())
Again, this will create an error, if you input a non-numerical value, so you need to handle the exceptions in such case.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
I'm working through some practice questions in the book "Automate the Boring Stuff," and I'm struggling to understand why the code below only produces 'Greetings!' as output.
print('What does spam equal? Hint: Try a number between 1 and 3')
spam = input()
if spam == 1:
print('Hello')
elif spam == 2:
print('Howdy')
else:
print('Greetings!')
It does because what you have given as input is stored as string in spam . And when you are using if else statements then it is comparing with integers, but your actual value is a string, therefore it always turns out to be in the final else statement printing Greetings!
So use spam=int(input()) instead.
This is because the input() returns a string, which is never equal to an integer.
Try
spam = int(input())
instead. This will of course throw a ValueError if the input is not an integer.
This question already has answers here:
Ordering of string representations of integers [duplicate]
(6 answers)
Closed 2 years ago.
Here's my code, super new to python. Struggling to understand why if I use < it always thinks is less than even though it will print a higher number. If I use greater than it works just fine. What am I missing? Here's my code, super new to python. Struggling to understand why if I use < it always thinks is less than even though it will print a higher number. If I use greater than it works just fine. What am I missing?
import time
t=time.localtime()
msttime=time.strftime("%H",t)
if(msttime < '2'):
print(msttime)
else:
print("This calculation believes msttime is greater than 2")
This code will give you the expected result:
import time
t = time.localtime()
msttime = time.strftime("%H", t)
if (int(msttime) < 2):
print(msttime)
else:
print("This calculation believes msttime is greater than 2")
The reason is that "18" < "2" lexographically, but 18 > 2 numerically. This is because the lexographical comparison has no regard for the second digit. Since 1 is before 2, the comparison ends there. In the numerical comparison, all digits are accounted for.
I am very new at python and i just wanted to make a pprogramm with a question how much is 23*10? and if the user answers 230 it will print true but if not it would print false and i dont know how to ... this is my first attempt
Question = input("How much is 23 * 10? ") if Question == "230":print("True")
It is pretty much what you made, lacking indentation and the else. Also, I'm adding int() before input() because otherwise, the value provided will be considered as a string instead of a number. It doesn't make much difference if you are then comparing it with a string "230" but I just thought I'll change it a bit to show you another perspective.
question = int(input("How much is 23 * 10? "))
if question == 230:
print("True")
else:
print("False")
As I explained before, if you do not wish to use int then you must compare the value to a string:
question = input("How much is 23 * 10? ")
if question == "230":
print("True")
else:
print("False")
This question already has answers here:
Prime Numbers python
(5 answers)
Closed 5 years ago.
I am supposed to write a program to change a string if that position number is not a prime number but I can't seem to figure out how to make the first position, for example position 2, prime, and make the rest of the positions that can be divisible by 2, turn into N. Below is what I currently have and I am an int error. If anyone can help me out, I would really appreciate it. Thank you
while True:
number = int(input("Enter a number greater than 10: "))
if number < 10:
print("Invalid input. Try again")
else:
break
n_list = ["P"] * (number + 1)
n_list[0] = "N"
n_list[1] = "N"
for i in range(n_list):
if int(n_list[i]) % 2 == 0:
n_list[i] = "N"
print(n_list)
Firstly, you probably get an error because range takes an integer, not a generator, try instead for i in n_list or for i in range(len(n_list)). As for the approach of the problem, you dont need to loop from 2 to n, there is a smarter way. But we are not here to solve that problem for you, only to guide you to find eventual errors that you may present us. With that said, best of luck to you!
PS:
A little more guidance
Sieve of Eratosthenes
EDIT:
The error i'm referring to is
TypeError: 'list' object cannot be interpreted as an integer
I don't want to give you the answer to your homework, but I do want to help so I will guide you a little.
I see 2 issues with what you've provided. First, you are using the incorrect variable for your range. You want a number, not a list.
Second, you cannot compare if "P" is evenly divisible 2. You can only check if a number is.
Hope this helps and good luck.
EDIT: To clarify, there are more issues, but these 2 will give errors.