'This is a piece of python program in which the operand j must be assigned a certain value from the keyboard
After the introduction of the operand j this operand must be in the range from 1 to 4 inclusive. If j is from 1 to 4, then the program goes further, otherwise the user will have to enter any number of any characters from the keyboard until j is equal to either 1 or 2 or 3 or 4.
j=input("enter a number from 1 to 4: ")
while(j.isalpha()==True and len(j)!=1):
while(int(j)!=1 or int(j)!=2 or int(j)!=3 or int(j)!=4):
j=input("please focus and enter the number from 1 to 4: ")
I think that such a program can be done using the while loop, that is, write several conditions in this loop. But I have a problem - how to write down these conditions?! +I have a constant confusion with or and and where to put what. Who knows, please tell me. It is advisable not to use functions other than those that are visible on the program.
Continuation I almost made this program, but there was a flaw. If you enter the required value of j, the program will still ask you to enter the value of j again and only after that the program will move on. I don't understand what's the matter, maybe who knows how to fix it?
j=input("")
while(len(j)!=1 or j.isalpha()==True or (int(j)!=1 or int(j)!=2 or int(j)!=3 or int(j)!=4)):
j=input("")
if(len(j)==1 and j.isdigit()==True):
if(int(j)==1 or int(j)==2 or int(j)==3 or int(j)==4):
break
continue
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
