+ 1
how to remove following run time error
program d={} n=int(input()) k="" t=0 for i in range(n): k=input() d[k]=input() print ("over") while True: k=input().strip() if k!='': print(k,"=",d[k],sep="") else: break; input 3 a 1234 b 2345 c 3459 a c b a https://code.sololearn.com/clWUPyFe0KVA/?ref=app
24 Réponses
+ 7
Try fixing line 13 now. Change `d[k]` to `d.get(k, "not found")`. This will solve the problem of trying to access keys that do not exist in the dictionary.
But I think the error you say is because the while loop is not breaking. You see, you need to input an empty string ('') for the break condition to be reached in your code (it is what the `else` means logically). On a real IDE you would simply hit the Enter key and that means "no input", thus an empty string (''). But here at SoloLearn you have to leave an entire line of input blank so that it is interpreted as an empty string.
+ 7
Worked just fine for me (even here at SoloLearn, that surprised me lol).
Make sure you leave a blank line at the end so your program breaks out of the while loop.
+ 6
You cannot ask for input dynamically at SoloLearn's code playground. Also, remove semicolon at line 14.
+ 6
Try changing line 14 to `print(k,"=",d.get(k, "not found"),sep="")`.
You will get KeyError if you try accessing a value with a non-existent key using `d[k]`. By changing to `d.get(k, "not found")` you ensure that no error will be raised because if the key `k` is not on the dictionary, then string "not found" will be returned instead.
Also, there is an unused variable `t` at line 4 that can be removed.
+ 6
On your IDE or here at the coding playground?
+ 6
What error exactly?
+ 6
How are you inserting your inputs?
+ 6
Satyavarapu chandra sekhar That's what's causing the error, you cannot input "a 1234" or "b 123". You have to do it in separate lines:
3
a
1234
b
123
c
345
b
a
c
#leave blank line at the end so it breaks out of the while loop
+ 6
Sure, you could replace lines 10 up to 13 with this:
if k == "quit":
break
else:
print(k,"=",d[k],sep="")
#now if you input "quit" it will break from the while loop
I also suggest for a better formatting on your print function. This would work better I think:
print(f"{k} = {d[k]}")
+ 6
It isn't because when using a while loop there is no way you can know how many inputs are being inserted.
You can change it to a for loop (just the same way you are asking for input to fill your dictionary at the start of the program) because for loops run for an exact known amount of times. That seems to be what you are wanting to do.
+ 5
Are you entering the inputs correctly? You should do it line by line. (One line for `n`, another for `k`, another for `d[k]`, ...)
Example input:
3
a
0
b
5
c
4
#while loop now
b
c
a
j #make sure to have changed line 14 to what I said or you get a KeyError here
'' #press enter (empty string) to quit the while loop
+ 5
I'm not sure what you mean. If you'd like to enter a limited amount of input when running your loop, you should change it to a for loop instead.
+ 2
thanks for resolving my issue.
output is perfect.but it shows an error after the ouput.
+ 1
but at console also it shows runtime error
+ 1
but it shows error in reading inputs dynamically
+ 1
IDE
+ 1
in above code it shows error while reading inputs dynamically
+ 1
still showing runtime error
+ 1
run the code
https://code.sololearn.com/clWUPyFe0KVA/?ref=app
+ 1
but there is no output