Skip to content
Sign UpLog In
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
2

Repeating in Python

aricmiller
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
)

2 years ago

Voters

Comments

TopNew
1
RYANTADIPARTHI
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

2 years ago
1
aricmiller
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?

2 years ago
1
RYANTADIPARTHI
RYANTADIPARTHI

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

2 years ago
1
aricmiller
aricmiller

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

2 years ago
4
RYANTADIPARTHI
RYANTADIPARTHI

@aricmiller
like this.

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

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

2 years ago
4
RYANTADIPARTHI
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 + ": " + ".")
2 years ago
1
Coder100
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"):
2 years ago
1
aricmiller
aricmiller

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

2 years ago
Load more