Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Each time I put 0 at the beginning of my 7 digit code it is ignored and not times by 3. I have a feeling that I need to change something from str() to int() (and vice-versa) but I may be wrong. I would be grateful for assistance in this matter.
Numeric literals starting with 0 are interpreted as being in base 8.
>>> int("755", base=8)
493
>>> 0755
493
>>> input("> ")
> 0755
493
Try using raw_input() instead of input(). Input() evaluates the user input as python code, where raw_input() evaluates the entry as entered.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 months ago.
Improve this question
I am trying to write a simple function that takes a list of number passed as an argument and prints how many positive number is in the list.
I can't seem to figure out what is wrong with the code here. Can someone please explain this.
You should return add instead of num. And you should initialize add outside the for loop.
lst = [1,2,3,4,-4,-3,-2,-1]
def count_positives(lst):
return sum(i > 0 for i in lst)
print(count_positives(lst))
the program above will print 4
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
I just don't know what to do...
The eval() built-in function is for executing things that are in a string format.
To fix the error you're getting, either remove eval() or put input("heyy: ") in quotes:
x = eval("input(\"heyy: \")")
(The backslashes before quote marks are for escaping the quote, otherwise the quote mark in the string in input() would close the quote that you've opened first)
Also, both the eval() and input() functions would return a string, you need to convert it to an integer to add 5 onto it:
x = int(x)
try using int
x = int(input("Enter an integer : "))
print(x+3)
using eval:
Python eval() function parse the expression argument and evaluate it
as a python expression and runs python expression(code) within the
program.
x = int(input("Enter the value : "))
print(eval(f"x+{3}"))
output :
$ python3 test.py
Enter the value : 5
8
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I found this code on the Internet and it works well, but why when you enter the value of zero, does not work
Why when I enter 0 the loop doesn't stop
(https://i.stack.imgur.com/cgva8.jpg)
Look at the while section of your code - because 0 divided by 2 will give a 0 mod always, then you append the number two to a list (li), then update n to be n/2 which is still 0 - then it loops again and again - as your while condition remains true forever...
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am working on the problem link
Code:
lenInput= int(input())
while lenInput:
temCnt=0
proInput = str(input())
lenCnt= len(proInput)
for i in range (lenCnt):
if (proInput[i] == '4') or (proInput[i] =='7'):
temCnt+=1
print(lenCnt -temCnt)
lenInput-=1
I am able to get the correct output for the use cases mentioned in the site,but while submitting my code.
It's throw an error "Wrong answer"
Can you please help me in understand ,why the error is thrown ?
Why do you str(input())? input() already gives you a string to begin with.
The only action needed here is changing any non-lucky digit into a lucky one. The amount of changes needed is the amount of non-lucky numbers:
for _ in range(int(input())): # loop over each test case
i = 0 # start counting at 0
for c in input(): # loop over each character of input testcase
if c not in ['4','7']: # if character not lucky, count it
i+=1
print(i) # print count
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
How can I get the number of codepoints in a string which may contain unicode characters 3 byte long. https://unicode-table.com/
For example for "I❤U" I would like to get 3.
Doing len(str) returns the number of bytes, so for the above example I would get 5.
Try to decode it in python2:
"I❤U".decode('utf-8')
Output: u'I\u2764U'
then len("I❤U".decode('utf-8')), it will be 3
In my env, I tried your code. But my result of len("I❤U") is 3.
>>> len("I❤U")
3
>>>