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
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 1 year ago.
Improve this question
Why am I getting the following error code when I run this code:
builtins.NameError: name 'string' is not defined
def explore_string():
get_input()
explore_chars(string)
sum_digits(string)
def explore_chars(string):
print("Original: ",string)
print("Length: ",len(string),"chars")
print("2nd char: ",string[1])
print("2nd last: ",string[2])
print("Switched: ",string[-3:]+string[3:-3]+string[0:3])
def sum_digits(string):
dig_sum=0
l=['1','2','3','4','5','6','7','8','9']
for i in string:
if i in l:
dig_sum+=int(i)
print("Digit sum: ",dig_sum)
def get_input():
string=input("Enter 10 or more chars ending with a period: \n-> ")
while(len(string)<10 or string[len(string)-1]!='.'):
string=input("-> Error! Try again: ")
return string
explore_string()
You need to change your explore_string like this:
def explore_string():
string = get_input()
explore_chars(string)
sum_digits(string)
The result value of get_input() should be stored in a variable string
Imentu's answer seems like the right solution. However, I want to add some minor tips that could help you to solve such problems by yourself. Because you might encounter them many more times in the future (at least I did).
The error code oftentimes contains a lot of information about the problem that you're facing. In your case the xxx is not defined means that you are referencing an object xxx which Python doesn't know about yet (it is not defined).
Whenever you encounter this, then there are 2 main things you should check
Did I forget to assign it? (which is the case here)
Did I make a typo? (e.g. if you typed strng = get_input())
If you combine this with the fact that the issue is centered around the word "string" and the approximate line number given by the error, then you should be able to find the issue.
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 6 years ago.
Improve this question
Hi I have the following text file on which I am using a csv reader:
number,obstacle,location,message
1,gobacktostart,8,Sorry but you've landed on a dangerous number, back to the start for you!
2,movetosquare42,matrix[1][0],The meaning of life is 42 or so they say, and that's where you're headed
I wish to (at the end) retrieve the number 8 from the row that starts 1,gobacktostart,8....etc.
My code is:
def gobacktostart():
with open("obstacles.txt","r") as f:
idnumber="1"
fReader=csv.reader(f)
for row in fReader:
for field in row:
if field==idnumber:
print(row[3])
player1position==row[2]
print(player1position)
and the undesired output however, is:
>>>
Sorry but you've landed on a dangerous number
1
>>>
I do need to read the value into the variable player1position in order to pass it on to another function at a different part of the program.
Any thoughts on solving this logic error? Why is it printing "1", when row[2] refers to the 8. Also, Row[3] seems to execute properly in the previous line....
You are checking for equality not assignment in player position == row[2]
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 6 years ago.
Improve this question
I have found the following code on the internet and it is meant to output the positions of the words in the list:
mylist
But it is not working, here is the code:
mylist="example string with spaces"
sentencelist=[]
for z in mylist.split(" "):
sentencelist.append(z)
wordlist=[]
for z in range(len(sentencelist)):
if sentencelist[z] not in wordlist:
wordlist.append(sentencelist[z])
wordpositions=[]
for i in range (len(sentencelist)):
for o in range(len(wordlist)):
if sentencelist[i]==wordlist[o]:
wordpositions.append(o+1)
wordlist=str(wordlist)
wordpositions=str(wordpositions)
inputFile=open("sentence.txt","w")
inputFile.write(wordlist)
inputFile.write("\n")
inputFile.write(wordpositions)
inputFile.close()
No error message comes out but it also doesn't work. Can someone expalin
For me, the script does successfully write a file sentence.txt with the content of wordlist and wordpositions.
If you want to have these printed out to console, as well, add:
print(wordlist)
print(wordpositions)
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.