+ 1

Why is it displaying 1 only??

numerals = { 1 : 'one', 2 : 'two', 3 : 'three', 4 : 'four', 5 : 'five', 6 : 'six', 7 : 'seven', 8 : 'eight', 9 : 'nine', } for i in numerals: x = int(input()) if x == i: print (numerals[i]) break

6th Jun 2021, 6:13 AM
Samarth Kulshreshtha
Samarth Kulshreshtha - avatar
12 Antworten
+ 3
you doesnt need to iterate, you just have to print the dictionary value for index inputed by user ^^ if you iterate, put the input outside of the loop, else your script expect an user input at each iteration ;P
6th Jun 2021, 6:16 AM
visph
visph - avatar
+ 4
In SL Playground you have to have put all input data before is the code executed: x = int(input()) for i in numerals: if x == i: print (numerals[i]) break better will be: i = int(input()) print (numerals[i])
6th Jun 2021, 6:22 AM
JaScript
JaScript - avatar
+ 1
Got it correct. Thanks visph and jascript.
6th Jun 2021, 6:25 AM
Samarth Kulshreshtha
Samarth Kulshreshtha - avatar
+ 1
Samarth Kulshreshtha The order of the statements is jumbled.... x = int(input()) should be before for i in numerals: if(x==i): print (numerals [I]) break
6th Jun 2021, 12:17 PM
Sanjay Kamath
Sanjay Kamath - avatar
0
From which value should I replace break?
6th Jun 2021, 6:17 AM
Samarth Kulshreshtha
Samarth Kulshreshtha - avatar
0
why do you want to replace break? if you avoid the loop, then you no more need to break... if you keep the loop, just move input outside of it (before)
6th Jun 2021, 6:20 AM
visph
visph - avatar
0
Ok, so keeping x outside for loop should do the work?
6th Jun 2021, 6:21 AM
Samarth Kulshreshtha
Samarth Kulshreshtha - avatar
0
yes, but you doesn't need the loop...
6th Jun 2021, 6:22 AM
visph
visph - avatar
0
do you have keep the loop? x = int(input()) print(numerals[x]) should work as well ;P
6th Jun 2021, 6:27 AM
visph
visph - avatar
0
Short tricks are very useful. Thank
6th Jun 2021, 6:28 AM
Samarth Kulshreshtha
Samarth Kulshreshtha - avatar
0
Instead try: i = int(input()) If i in numerals.keys(): print(numerals[i]) In case you wanna check if the input is actually a key in the dictionary.
7th Jun 2021, 11:17 AM
Rayshawn
Rayshawn - avatar
- 1
x = int(input()) numerals = { 1 : 'one', 2 : 'two', 3 : 'three', 4 : 'four', 5 : 'five', 6 : 'six', 7 : 'seven', 8 : 'eight', 9 : 'nine', } for i in numerals: if x == i: print (numerals[i]) break #Now you can run program and check it
8th Jun 2021, 2:41 AM
Abhishek Kumar
Abhishek Kumar - avatar