I wrote a code and want to end it after the first input is equal to "done".
If I write the condition after all of the inputs, the user should answer all the useless questions.
On the other hand, I don't want to write the condition in the middle of the inputs as you see in the following part.
I would appreciate it if anyone could help me with this.
Here is the code:
while True:
ind1=input('please enter your personal number:')
if ind1=='done':
break
ind2=input('please enter your name:')
ind3=input('please enter your family name:')
ind4=int(input('please enter your working hours:'))
ind5=int(input('please enter your payment:'))
Instead of using multiple variables, use an array.
pseudo code:
inputs = []
count = 0
while true:
if inputs[count] == 'done': break
inputs[count] = input('...')
count++
I am really having a hard time with loop statements for python, please see below:
I want it to check the users age, so if they are over 18, it will say "old enough, if they are over 16, it will say "almost there" and if they are younger than this, it will say "sorry you're too young"
I got the following code below:
age = int(raw_input("Enter your age:"))
if age >= 18:
print "You are old enough!"
elif age >= 16:
print "Almost there"
else:
print "You're just too young"
my problem is, how do I write code the includes an if statement and a for loop that will print out all the numbers from 0 that are less than the user's inputted age?
do I say:
for age in range (0,num)
if num <= age
print ....
else:
print...
Please kindly help. I'm new at this and still learning :(
So you could construct a variable that contains a list of numbers that is smaller than the user's age. You can construct this using python's range function; something like range(age) would do this nicely.
Then you need to use a for loop to loop through all these numbers and print them to the command line. Something like for x in range(age) would be a good place to start.
Then, in the loop, you'd just need to turn x from an integer into a string using the str() function, and print it to the command line.
So as pseudo-code:
if the age is greater than 18:
print that they're old enough
else if the age is greater than 16:
print that they're nearly old enough
otherwise:
print that they're not old enough.
for each number between 0 and age:
print the number
Come back when you have some code and we can help you with any trouble you have.
I am totally new to programming and I have been trying to get a simple piece of code working. However I keep getting multiple different errors so I assume I am doing something very wrong somewhere along the line... Here is my code at the moment:
userName = input('Please enter your name: ')
age = input('Please enter your age: ')
if int(age) <= 5:
print(userName, 'you are too young to play') break
else:
print (userName, 'Your old enough')
factor = 2
finalAge = int(age) + int(factor)
multAge = int(age) * int(factor)
divAge = float(age) / int(factor)
print('In', factor, 'years you will be', finalAge, 'years old', userName )
print('Your age multiplied by', factor, 'is', multAge)
print('Your age divided by', factor, 'is', divAge)
What I want to do is, if the user's age is not higher than 5, then they get the message that they are too young to play and it returns to the start of the piece of code - asking for the name again.
Does anyone have any advice on how to do this?
You'll need to use a loop. Syntax depends on language, which you haven't specified. As pseudo-code, you could do
loop indefinitely
prompt for name and age
if age is less than 5
print error
otherwise
print that age is ok
break loop
Take a look at while loops. For this you'd be able to set some kind of condition (such as an "old_enough" variable and when that becomes true, the loop stops running.
You would set this value inside the if statement. By default it should be whatever will make the loop run
There's loads of tutorials online for this, but here's an example in python (your code sample looks like Python3):
old_enough = False
while not old_enough:
# do something
if age > 5:
print("You're old enough")
old_enough = True
else:
print("you're not old enough")
That should make sense. if not, please do look up documentation. It'll be better for you in the long term
I'm trying to create a basic maths program which will randomly generate a numerical question and then allows 3 attempts to complete it before it moves onto the next question however cant figure out how to make it do both together.
My code currently looks like this
print "What is your name?"
score = 0
Attempt = 0
loop = True
Name = raw_input()
import random
for i in range (1,6):
question_1_part_1 = random.randint(1,30)
question_1_part_2 = random.randint(1,30)
print "What is", question_1_part_1, "+", question_1_part_2, "?"
while Attempt <3: # inputing this while loop here means it will retry the same question.
Guess = raw_input()
Guess = int(Guess)
answer = question_1_part_1 + question_1_part_2
if Guess == answer:
print "Well done"
score = score + 1
else: print "try again"
Attempt = Attempt + 1
if Attempt == 3: print "You failed"
print Name, "your score is", score
A simple break statement will take you out of the loop when the answer is correct.
if Guess == answer:
print "Well done"
score += 1
break
else: print "try again"
Note the change to your assignment; this is considered a cleaner increment.
To answer your comment ... you don't use this when the person gets the question wrong. Your program logic freezes out the user after three wrong guesses total, on all questions. If you want them to have up to three guesses on every problem, then you have to reset the Attempt counter for every question. Pull that line into the outer loop:
for i in range (1,6):
Attempt = 0
loop = True
question_1_part_1 = random.randint(1,30)
question_1_part_2 = random.randint(1,30)
In the future, I strongly recommend that you program incrementally: get a few lines working before you add more. Insert tracing print statements to check that the values and program flow are correct. Don't remove them until that part of the code is solid -- and even then only comment them out until the entire program is working. Some of your problems stem from trying to write more than you can comfortably hold in your mind at once -- which is common at most levels of programming. :-)
Add another argument to your while loop.
while Attempt <3 and Guess != ... :
rest of loop code
Then you are exiting your loop when they get the correct answer. Or you could break from the statement when they get the answer right.
I am receiving a formatting error from an input file, and I would like to determine where the formatting error is occurring in the input file.
My question is: Is there a way to print the line number of the input file where the error is occurring in my fortran code?
This is the error I get:
fmt: read unexpected character
apparent state: unit 4 named input_file
last format: (6(I3,X,F7.2,X,I2,X))
lately reading sequential formatted external IO
Which means that the code is getting hung up on some line in my input file that doesn't correspond with the above format.
Here is the part of my code where Fortran is reading in the input file:
DO 20 K = 1,NUMB
READ(4,200) (NSTN(J),TT(J),IKPS(J),J=1,6)
DO 30 J = 1,6
L = L+1
ISTO(L,N) = NSTN(J)
SECT(L,N) = TT(J)
KWV(L,N) = IKPS(J)
30 CONTINUE
20 CONTINUE
KOBS(N) = NSTM
10 CONTINUE
100 FORMAT(5(I2,X),F6.2,X,F5.2,X,F7.3,X,F6.3,X,F8.3,X,F6.3,
& X,F6.2,X,F5.2,X,I3,X,F4.1,X,F5.2,X,F7.3)
200 FORMAT(6(I3,X,F7.2,X,I2,X))
RETURN
END
I'd like to add a line in the above piece of code to identify the current line the code is reading, so that when it gets hung up, I'll know which line contains the error. Thank you for your help.
Here's what I tried and it's giving me another error:
c READ(4,200) (NSTN(J),TT(J),IKPS(J),J=1,6)
READ(4,200)line
READ(line,*,iostat=ios) (NSTN(J),TT(J),IKPS(J),J=1,6)
IF(ios>0)THEN
WRITE(*,*)'Error Reading Line',line
STOP
ENDIF
INTEGER ios
CHARACTER*(200)line
With a read statement like
READ(4,200) (NSTN(J),TT(J),IKPS(J),J=1,6)
an error in the input results in (error) termination of the program. One has no control over this termination, and in particular one can't do further processing.
There are two ways to avoid this termination, and both involve using an additional specifier in the read statement. One is iostat= and the other err=. If either of these is present then an error doesn't result in termination.
With iostat (for integer istat):
READ(4,200,iostat=istat) (NSTN(J),TT(J),IKPS(J),J=1,6)
then on an error condition, istat will have a (processor-dependent) positive value. It will be zero when (and only when) there is no error.
With err (for some label, say, 991):
READ(4,200,err=991) (NSTN(J),TT(J),IKPS(J),J=1,6)
Putting all that together, let's imagine an outer loop
DO 100 LINE=1,91959
READ(4,200,IOSTAT=ISTAT) (NSTN(J),TT(J),IKPS(J),J=1,6)
IF (ISTAT.NE.0) THEN
PRINT *, 'It went wrong on line', LINE
STOP
END IF
...
100 CONTINUE
or
DO 100 LINE=1,91959
READ(4,200,ERR=991) (NSTN(J),TT(J),IKPS(J),J=1,6)
...
100 CONTINUE
...
991 PRINT *, 'It went wrong on line', LINE
STOP
[I couldn't bring myself to write that code like it really was 1980.]
Add an explicit loop to read your data like:
DO 20 J = 1,6
write (*,*) 'Reading line = ', J
READ(4,100) NSTN(J),TT(J),IKPS(J)
20 CONTINUE
100 FORMAT(I3,X,F7.2,X,I2,X)
This way, you will know exactly where it stopped thank to the write statement in the reading loop. Note that I added two new labels, 20 to control the new loop and 100 for the new format statement. Adapt accordingly.
==============
DO 20 K = 1,NUMB
WRITE (*,*) 'Reading line = ', K
READ(4,200) (NSTN(J),TT(J),IKPS(J),J=1,6)
DO 30 J = 1,6
L = L+1
ISTO(L,N) = NSTN(J)
SECT(L,N) = TT(J)
KWV(L,N) = IKPS(J)
30 CONTINUE
20 CONTINUE
KOBS(N) = NSTM
200 FORMAT(6(I3,X,F7.2,X,I2,X))
RETURN
END