Skipping print but continuing program
Your code looks perfect!
Just need some changes according to (python 3)
def get_index():
while True:
index_in = int(input("Enter a number between 0 and " + str(len(string) - 1) + " (-1 to quit): "))
if index_in > len(string) -1 or index_in < -1:
print("Invalid Index")
elif index_in == -1:
return False
else:
get_letter(index_in)
def get_letter(index):
global string
char_list = list(string)
while True:
letter = input("Enter a letter: ")
if letter.isupper():
print("Character must be lower case!")
else:
char_list[index] = letter
string = ('').join(char_list)
break
print(string)
return
string = input("Enter a word: ")
get_index()
You should use print(msg) instead of print msg in python 3
You missed one condition, consider this scenario,
Run the program
Enter a word: c
Enter a number between 0 and 0 (-1 to quit): 1
Error
there was a wrong in get_index() check on line:
if index_in > len(string) or index_in < -1:
it should be :
if index_in > len(string) -1 or index_in < -1:
- ============================================================ *
UPDATE
updating the annwer according to discussion in comments:
def get_index(string):
try:
while True:
index_in = int(input("Enter a number between 0 and " + str(len(string) - 1) + " (-1 to quit): "))
if index_in > len(string) -1 or index_in < -1:
print("Invalid Index")
elif index_in == -1:
return False
else:
get_letter(index_in, string)
except ValueError:
print("Please Enter a number!")
get_index(string)
def get_letter(index, string):
char_list = list(string)
while True:
letter = input("Enter a letter: ")
if letter.isupper():
print("Character must be lower case!")
else:
char_list[index] = letter
string = ('').join(char_list)
break
print(string)
return
if __name__ == "__main__":
string = input("Enter a word: ")
get_index(string)
CHANGES are:
You must avoid global variables.
Instead, pass particular variable to functions as argumens.
Notice, howstring
was used in first answer and how it was used in the updated answer.Use try-catch blocks to catch unexpected behavior
I'm trying to write a code in which the character "|" is inbetween each letter however it does not work for some characters
Thanks for all your suggestions; it has really helped with the troubleshooting, and the problem in general, and I managed to fix it, so it is all good.
Here is my finished code:
def art(word):
numoflet=len(word)
word=word.upper()
for Y in range(numoflet-1,-1,-1):
let=word[Y]
let1=let+"|"
word=word[:Y]+word[Y].replace(let,let1)+word[Y+1:]
print(word)
print(Y)
word="|"+word
pat=""
numoflet=len(word)
for X in range(numoflet,0,-1):
if X%2==0:
pat=pat+"-"
else:
pat=pat+"+"
print("\n"+pat+"\n\n"+word+"\n\n"+pat)
#maincode
art("Craig'n'Dave")
How can I make python repeat a certain line of code until a user says they will keep the name?
You could run it in a loop until the user gives the input you desire, like:
surity = ""
while surity != "Yes":
nameofbot = input("What would you like to name your rover? ")
surity = input("Are you sure?")
This will keep on running until surity = "Yes" when it will escape the loop.
Related Topics
No Matching Distribution Found for Tkinter
Import Error: Dll Load Failed in Jupyter Notebook But Working in .Py File
Xlsxwriter: How to Open an Existing Worksheet in My Workbook
How to Calculate Range Between the Dataframe Values Using Python
Python Tkinter How to Update a Text Widget in a for Loop
Is There an Easy Way in Python to Wait Until Certain Condition Is True
Python: How to Split a List Based on a Specific Element
How to Tell Python to Convert Integers into Words
Unit Testing a Method With No Return Value
Number of Common Letters in Two Strings
How to Generate and Open an Outlook Email With Python (But Do Not Send)
Python Regex - Finding Phone Number
Sum Numbers of Each Row of a Matrix Python
Grab a Number After a String in a File
Turn String into a List and Remove Carriage Returns (Python)