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
Related
This question already has answers here:
Understanding negative steps in list slicing
(2 answers)
Closed 1 year ago.
I am learning python and I can't solve this problem:
I am ok to reverse the whole string but I want to get only the "Hello" part
astring = "Hello world!"
I was expecting print(astring[0:4:-1]) would do the work but it does not.
print(astring[5:0:-1]) is better but the H is still missing. I get "olle"
Is there anyway to solve this?
Thank you,
Try this:
print(astring[4::-1])
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:"))
This question already has answers here:
What does return mean in Python? [closed]
(2 answers)
Why is "None" printed after my function's output?
(7 answers)
Python: Why "return" won´t print out all list elements in a simple for loop and "print" will do it?
(4 answers)
Closed 5 years ago.
I have been practising python and have a small question. I am working with DNA sequences and so I simply wanted to make a small function that just returned the record.ids.
from Bio import AlignIO
my_alignment = Align.IO.read("multipleseqfile.fa","fasta")
def get_id_names(alignment):
for record in alignment:
return record.id
print get_id_names(my_alignment)
I had done a for loop before that prints the names nicely but I wanted to improve my script and make these exercises into functions. However, when I use this function, it only returns the first record id (and there is a list of 30-40). I switched the return record.id to print record.id, and it does print all the names but then I get a None at the end of the output. Not sure what is going on here?
This question already has answers here:
Python indentation mystery
(3 answers)
Closed 6 years ago.
I am a code beginner.Can anyone tell me what happend if I get a syntaxerror when putting the "print" outside the loop
Seems like you are using an interactive interpreter shell. You should hit enter after the last line of the loop before you try to print.
In future questions please write your code in the question body instead of attaching a screenshot.
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).