Now, I'm nearly done with the grocery problem, but I just need to for example, if i input Apple Orange at the same time to print in separate lines...It prints in separate lines if I type Apple then enter, Orange then enter... but if I type Apple Orange then enter prints one next to the other...
I love learning math (*to some extent*) and CS theory; good literature, and the technology that provides me with all kinds of resources for those three: the web.
Not sure if I understand it correctly, but shouldn't you enter each item separately anyway? Like the example I give in this article, Accio, Lumos, etc. all inputted one by one. So if you put two items as one input, it is going to be stored as such.
If i input one by one my code works correctly although I only get 2 green checks that the file exists and the one handling the EO error... the other errors are all the same:
:( input of "apple" and "banana" yields "1 APPLE 1 BANANA"
expected "1 APPLE\n1 BAN...", not "Input: 1 APPLE..."
:( input of "strawberry" and "strawberry" yields "2 STRAWBERRY"
expected "2 STRAWBERRY", not "Input: 1 STRAW..."
:( input of "mango", "sugar", and "mango" yields "2 MANGO 1 SUGAR"
expected "2 MANGO\n1 SUG...", not "Input: 1 MANGO..."
:( input of "tortilla" and "sweet potato" yields "1 SWEET POTATO 1 TORTILLA"
expected "1 SWEET POTATO...", not "Input: 1 TORTI..."
below is a example of my logic...
dictionary = {}
while True:
user_input = input("Input: ")
dictionary[user_input] = dictionary.get(user_input, 0) + 1
for key, value in sorted(dictionary.items()):
print(f"{value} {key.upper()}", end=" ")
print("\n")
I love learning math (*to some extent*) and CS theory; good literature, and the technology that provides me with all kinds of resources for those three: the web.
I implemented it with the same logic as well. But, you need to wrap everything inside a try...except block. Like, you can try to get the input and put it inside the dictionary; and only inside except EOFError you can do the for loop and printing. Also, I don't think you need to specify end inside the first print function and then print a newline. Just print(f"{value} {key.upper()})" will be going to provide a new line after each time by default, so you do not need to do anything else.
Amazing! I fixed it! Thanks a lot for the tip...I was around and around trying new ways and would never have guessed that the for loop should be inside the except. I even tried import sys and use sys.stdin.read().split("\n") and was working as expected, but couldn't get green checks, only yellow checks ...anyway, now looking through the code and thinking about the logic makes perfect sense...Thanks a lot! Onto the next one :)
I love learning math (*to some extent*) and CS theory; good literature, and the technology that provides me with all kinds of resources for those three: the web.
Congrats! We only need to print everything after the user hits control-d (resulting in EOFError), so it makes sense to put it inside the except. It's absolutely great to see that helped!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Now, I'm nearly done with the grocery problem, but I just need to for example, if i input Apple Orange at the same time to print in separate lines...It prints in separate lines if I type Apple then enter, Orange then enter... but if I type Apple Orange then enter prints one next to the other...
Not sure if I understand it correctly, but shouldn't you enter each item separately anyway? Like the example I give in this article, Accio, Lumos, etc. all inputted one by one. So if you put two items as one input, it is going to be stored as such.
If i input one by one my code works correctly although I only get 2 green checks that the file exists and the one handling the EO error... the other errors are all the same:
:( input of "apple" and "banana" yields "1 APPLE 1 BANANA"
expected "1 APPLE\n1 BAN...", not "Input: 1 APPLE..."
:( input of "strawberry" and "strawberry" yields "2 STRAWBERRY"
expected "2 STRAWBERRY", not "Input: 1 STRAW..."
:( input of "mango", "sugar", and "mango" yields "2 MANGO 1 SUGAR"
expected "2 MANGO\n1 SUG...", not "Input: 1 MANGO..."
:( input of "tortilla" and "sweet potato" yields "1 SWEET POTATO 1 TORTILLA"
expected "1 SWEET POTATO...", not "Input: 1 TORTI..."
below is a example of my logic...
I implemented it with the same logic as well. But, you need to wrap everything inside a
try...except
block. Like, you can try to get the input and put it inside the dictionary; and only inside except EOFError you can do the for loop and printing. Also, I don't think you need to specifyend
inside the firstprint
function and then print a newline. Justprint(f"{value} {key.upper()})"
will be going to provide a new line after each time by default, so you do not need to do anything else.Ye I have everything wrapped inside try...except block, just didnt include that in the code I wrote here...
Amazing! I fixed it! Thanks a lot for the tip...I was around and around trying new ways and would never have guessed that the for loop should be inside the except. I even tried import sys and use sys.stdin.read().split("\n") and was working as expected, but couldn't get green checks, only yellow checks ...anyway, now looking through the code and thinking about the logic makes perfect sense...Thanks a lot! Onto the next one :)
Congrats! We only need to print everything after the user hits control-d (resulting in EOFError), so it makes sense to put it inside the
except
. It's absolutely great to see that helped!