This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
So I am not great with Python if it is not obvious, but I can't seem to figure out why it is straight up ignoring my if statement. Could anyone please shed some light on this, I have double checked my variable names.
I have more code above this that imports random and does some password checking successfully, but this section of code isn't working as expected.
correct=0
q1a=random.randint(0,10)
q1b=random.randint(0,10)
answer1=q1a+q1b
print(q1a, "+", q1b, "=")
a1=input("Answer: " )
if answer1==a1:
print("Correct!")
correct+=1
print(answer1,a1,correct)```
Typecast input to integer, by default it takes it as a string.
a1 = int(input("Answer:"))
Related
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 2 years ago.
I've begun my first scripting class. I'm am currently stuck in the string formatting section.
The instructions for this problem are, "Write a single statement to print: user_word,user_number. Note that there is no space between the comma and user_number."
They provide part of the code to start,
user_word = str(input())
user_number = int(input())
I've had trouble getting errors combining strings and integers in single statements and I am a bit lost on where to start on this. This is also my first time on stack overflow.
You can do it like this
print(user_word+","+str(user_number))
It should work.
This way, you cast you int to string and then you concatenate.
This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 2 years ago.
Why is a string with a minus sign not recognised properly when passed to a function in python? The simple code
def f(s):
print(s)
if s is 'a-b':
print('I expect this to be printed.')
else:
print('Instead, this is printed.')
s = 'a-b'
if s is 'a-b':
print('Oustide everything is fine.')
f(s)
gives the output
Oustide everything is fine.
a-b
Instead, this is printed.
Can someone explain please why this happens? Without the minus sign everything is fine.
Python distinguishes between values that are equal and values that are identical. You should use the is operator rarely. In fact, you probably won't encounter too many cases where you use is other than is None.
Here, you want if s == 'a-b':.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I tried writing a code for the collatz func .But somehow i failed in it.i am just sharing the code which i tried.can you figure out the mistake
def my_input():
a=input("enter:")
collatz(a)
def myprint(y):
print(y)
if (y!=1):
my_input()
def collatz(number):
if (number%2)==0:
return myprint(number/2)
else:
return myprint(3*number+1)
my_input()
Your mistake is int(input("enter:")), because you are passing in a string into your function collatz(number), without converting it into int.
Welcome to stackoverflow, next time please include things like your expected outputs or what error you are receiving so that people can easily help you. You can read about how to ask a question here
This question already has answers here:
Understanding slicing
(38 answers)
Closed 4 months ago.
I am trying to make a program that only prints positions in a string that is greater than this position in the string, first i tried the > but it comes up with "Invalid syntax" and highlights it in red. I tried the comparison greater than but that does the same thing (Because i am not comparing)
What i have tried:
sentence = ("Mark")
print (sentence[> 1])
What i want it to print:
rk
If you have any solutions or alternatives to a greater than operator in Python please let me know and i will give it a go. :)
All you need to do is print (sentence[2:])
For future reference and research, it is called slice.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
Could someone please help me to understand why the program doesn't output 'Well done!' after I input 1? Thank you.
while True:
o=input("Enter 1:")
if o==1:
break
print("Well done!")
It looks like you're using python3.x.
On python3.x, input always returns an instance of str, so o will never equal the 1 since you're comparing different types (str and int).