+ 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
12 odpowiedzi
+ 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
+ 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])
+ 1
Got it correct. Thanks visph and jascript.
+ 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
0
From which value should I replace break?
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)
0
Ok, so keeping x outside for loop should do the work?
0
yes, but you doesn't need the loop...
0
do you have keep the loop?
x = int(input())
print(numerals[x])
should work as well ;P
0
Short tricks are very useful. Thank
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.
- 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