0

I want to create a counter that counts up when inputting "drin-king"

#some help needed here, would be nice #i wanted to built a counter which counts up from 0 when the user inputs the string "drin-king". so rarher the output should count up #but obviously i need to get the x reset to count it up right? #the second problem is how can i count up, because for that the input needs to be inside the def, or not? and when i start counter more than once it stays at 1... number=str(input()) def counter(number): x=0 if number == "drin-king": x=x+1 print ("the score is currently at",x) return x #so is that right here to return x? else: print("you stupid mopfer typed",number,"instead of drin-king") return number #the second problem, the input needs to be reset? counter(number) counter(number) counter(number) #so in general the problem is: x needs to be saved at the current number and the function counter needs to run completely when using it #i woyld be happy if someone could comment a solution. thanks mates. https://code.sololearn.com/clzRb5ZNTTz3/?r

23rd Apr 2022, 7:22 PM
Patrit
Patrit - avatar
9 odpowiedzi
+ 1
Not clearly understanding ... you want the counter() function to increment and return some value when given argument was "drin-king", and instead return zero otherwise; is that it?
23rd Apr 2022, 9:16 PM
Ipang
+ 1
Wait a second, so you want the program to remember the last count from yesterday or last run? for that purpose I guess you would probably need to use a file as storage. You will have to save the number into the file when program exits, and read back from it next time program runs.
23rd Apr 2022, 9:42 PM
Ipang
+ 1
No it's not a mistake, just one difference. If we store data in variables, they are gone when program has completed. If we store data in file then they remain unless we delete the file. But since SoloLearn is a sandbox, any file created during program runtime will be deleted as the program completed.
23rd Apr 2022, 10:22 PM
Ipang
+ 1
I posted a comment in your code, an approach using class property as a value to be changed by counter() function. * File related chapters in Python core. https://www.sololearn.com/learn/Python/2445/ https://www.sololearn.com/learn/Python/2446/ https://www.sololearn.com/learn/Python/2447/ https://www.sololearn.com/learn/Python/2448/
24th Apr 2022, 6:57 AM
Ipang
+ 1
Just post it here. If and when I can, I'll see what I can do ...
24th Apr 2022, 7:19 AM
Ipang
0
Well simply i want to Count how often I right "drun-king". And the number or rather the value should be saved. So if I input drun-king it should output 1 when I input drun-king a day later it should output 2 and so on. So when I input drun-king 15 times it should output "the score is currently at 15" when I write something else than drun-king it shouldn't count. For example: drun-king drun-king drun-king Donkeykong drun-king drun-king Should output "the score is currently at 1" "the score is currently at 2" "the score is currently at 3" "You stupid mopfer typed Donkeykong instead of drun-king" "the score is currently at 4" "the score is currently at 5"
23rd Apr 2022, 9:34 PM
Patrit
Patrit - avatar
0
Oh alright, I thought a alternative is just to call the function that often. Like in the code I call it 3 times, i thought that would save the number 3 when the input is 3 times drun-king. But apart from that the counter doesn't counts anything till now... how to solve this. Thx btw I thought I can store variables and values in python directly, a big mistake ;-)
23rd Apr 2022, 9:52 PM
Patrit
Patrit - avatar
0
Alright have you some code or can you link me some with storing variables in data file? And still my counter doesn't count, can you or someone may make a better version of this code?
24th Apr 2022, 6:48 AM
Patrit
Patrit - avatar
0
Thx i look what i can do, when i get that, can i contact you again for the other Problems of this counter?
24th Apr 2022, 7:13 AM
Patrit
Patrit - avatar