Skip to content
Sign upLog in
← Back to Community

Repeating in Python

Profile icon
aricmiller

Hello, world! I am currently working on a AI and I would like to repeat the code,

if UserInput == "hi": print(AI_Name + ": Hello!") elif UserInput == "who made you": print(AI_Name + ": I was made by Aric Miller") if UserInput == "who are you": print(AI_Name + ": I'm " + AI_Name + "! I can do lots of things.")

But I am not sure how to do that. Could you help me?
Thanks in advance!
-Aric Miller (

@aricmiller
)

Answered by RYANTADIPARTHI [earned 5 cycles]
View Answer
Voters
Profile icon
aricmiller
Comments
hotnewtop
Profile icon
RYANTADIPARTHI

Solution

use either a while loop or a for loop. Based on what's good. for loops repeat for limited, and while do it forever, until the something's met.

  • for
for i in range(10): if UserInput == "hi": print(AI_Name + ": Hello!") elif UserInput == "who made you": print(AI_Name + ": I was made by Aric Miller") if UserInput == "who are you": print(AI_Name + ": I'm " + AI_Name + "! I can do lots of things.")

that repeats for 10 times.

  • while
while True: if UserInput == "hi": print(AI_Name + ": Hello!") elif UserInput == "who made you": print(AI_Name + ": I was made by Aric Miller") if UserInput == "who are you": print(AI_Name + ": I'm " + AI_Name + "! I can do lots of things.")

that repeats, while it's true.
That should work

Profile icon
aricmiller

@RYANTADIPARTHI
Alright, thanks!

Profile icon
RYANTADIPARTHI

@aricmiller
no problem.

Profile icon
aricmiller

@RYANTADIPARTHI
So I added the code

UserInput = str.lower(input(": ")

inside the "while True" loop but now it brings up an error. Do you know why?

Profile icon
RYANTADIPARTHI

@aricmiller
can i see your repl?

Profile icon
aricmiller

@RYANTADIPARTHI
Sure, edited.

Profile icon
RYANTADIPARTHI

@aricmiller
ok, your error is, you didn't put a : after the True. And you didn't indent it properly. Do those things.

Profile icon
aricmiller

@RYANTADIPARTHI
What's the proper way to indent it? Can you show me a codeblock?

Profile icon
RYANTADIPARTHI

@aricmiller
like this.

while True: UserInput = str.lower(input("")

also, on the line 17, you forgot a ). Put that.

Profile icon
aricmiller

@RYANTADIPARTHI
Okay, thanks!

Profile icon
RYANTADIPARTHI

@aricmiller
here's the code. with no errors.

while True UserInput = str.lower(input("")) if UserInput == "hi": print(AI_Name + ": Hello!") elif UserInput == "who made you": print(AI_Name + ": I was made by Aric Miller") if UserInput == "who are you": print(AI_Name + ": I'm " + AI_Name + "! I can do lots of things.") if UserInput == "what time is it" or "what is the time" or "time": print(AI_Name + ": " + ".")
Profile icon
Coder100

Try this code:

while True UserInput = str.lower(input("") if UserInput == "hi": print(AI_Name + ": Hello!") elif UserInput == "who made you": print(AI_Name + ": I was made by Aric Miller") if UserInput == "who are you": print(AI_Name + ": I'm " + AI_Name + "! I can do lots of things.") if UserInput == "what time is it" or "what is the time" or "time": print(AI_Name + ": " + ".")

remember to indent lol!

also, this code will always be true:

if UserInput == "what time is it" or "what is the time" or "time":

that's because python makes it

if (UserInput == "what time is it") or ("what is the time") or ("time"):

a string is always true, so this will always be true.

so change it to

if UserInput in ("what time is it", "what is the time", "time"):
Profile icon
aricmiller

@Coder100
Thanks! It's still giving me an error at line 18 though.