I'm running python 3 in the default IDE.
Here is my code:
def print_():
f = open("height.txt","r")
content = f.read()
print(content)
f.close()
main()
def main():
name = str(input("What is your name?"))
data = int(input("How tall are you? (CM)"))
if data.isdigit() == True:
print("\n")
elif data.isdigit() == False:
print("Must be a number!")
main()
# a+ is read only mode
f = open("height.txt","a+")
f.write(name)
f.write(str(data))
f.write("cm, ")
f.close()
data_1 = str(input("1 = View Contents. Other = Quit"))
if data_1 == '1':
print_()
else:
print("Exiting")
main()
You're trying to check isdigit()
on data you already casted to int. Read it in as a str
then cast it once you've checked it's a digit:
def main():
name = str(input("What is your name?"))
data = str(input("How tall are you? (CM)"))
while !data.isdigit():
print("Must be a number!")
data = str(input("How tall are you? (CM)"))
print("\n")
data = int(data)
print("\n")
this loop will continue to ask for an int until provided with one, then cast it as int
The issue is that isdigit operates on a string; however you cast the data object to an int already: data = int(input("How tall are you? (CM)")). You should let the data be a string if you want to use the isdigit function. data = str(input("..."))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.