Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+6 votes

I am having a difficult time finding the correct code for number 5 of routine practice, where I am confused on what is needed in order to repeat n times. I keep getting errors with the codes I enter.

times=input("How many times to repeat? ")
for i in range()
print ("I love Python!")

I know this code is off but this is some of my thought process, what am I missing?

asked in CSC201 Spring 2021 by (8 points)
0

Note: in Python, the indenting matters, but sometimes that doesn't come across in the Q&A. To make sure the indenting is preserved, when you type/copy code into your Q&A question post, highlight it and click the { } button to make sure you are formatting it as code. For example, did you write it like this:

times=input("How many times to repeat? ")
for i in range()
    print ("I love Python!")

or like this (without the print statement being indented):

times=input("How many times to repeat? ")
for i in range()
print ("I love Python!")
+1

FIrst, when you want to input a number, you need the code

 int(input('how many times to repeat?'))

you need the int before the input to help the program understand that the input is an interger not a word.

Second is that you need to indent the sentence under

for i in range():
       print('')

4 Answers

+5 votes

a/ The problem of the 5th quiz is to repeat an unknown number of times from the input user. therefore, there is no specific number entered in range () syntax.

b/The loop function requires you to enter an integer value. Since the option of entering a specific number is excluded, you could enter a variable within integer value in the range () syntax.
The code will turn out like this for i in range(variable):
Ex:

for i in range(times):

*Remember to add a colon (:) too.

c/However, there would be errors if you forget to convert the entered value to an integer by using the input function for integer number:
variable = int(input('How many times to repeat? '))
or
variable = eval(input('How many times to repeat? '))
Ex:

times = int(input('How many times to repeat? '))

d/ Another error is that you forget to indent the print() line that you want to get repeated.
The full repeating code is

for i in range(variable/number):
     print('words describing what to enter')

not like this
for i in range(variable/number):
print('words describing what to enter')

So your loop code should be like this:

for i in range(times):
     print('I love you. Happy Valentine!')

e/I could show the full fixed code for you, but I am not allowed to do so (by prof). Sorry, girl, but I truly don't know how to explain this one for you to understand best. I did break it down into each step within each error.

Hope that you will get what I mean to do your quiz.

answered by (8 points)
edited by
+1

Great step-by-step explanation! The only thing I think you missed is that the posted code was also missing a colon : at the end of the line that starts with for, like Elizabeth mentioned in her answer.

+1

This really helped me understand how to correctly figure out the code! Thank you!!

+3 votes

There's a really good example on page 13 of our textbook. There's a piece of code about half way down the page that if you use that as a guide it should work.
(Don't forget the : at the end!)

answered by (8 points)
+3 votes

input() by itself saves the variable (times) as text. you need to use eval() or int() to convert it to a number.

for example,

number = input("give me a number!") 

would take whatever is input and save it as text.

number = int(input("give me a number!") )

would take whatever is input, try to convert it into an integer(number) which can be used by the program, and save that.

functions that ask for numbers can only understand the second example.

also, your range(): function needs an input, like range(number):.

note: eval() and int() function the same.

answered by (8 points)
edited by
+1

Note: I edited your answer a bit to use the { } button to format the code as code.

+2 votes

I had the same problem, I figured it out by looking back at day 1 slides, your problem seems to be on line 1.

answered by (8 points)
...