How would I solve these python problems?
A) File Display
Download the file from here named numbers.txt . Write a program that displays all of the numbers in the file.
numbers.txt contains
22
14
-99
AB
B) Error Check Float Input
When you want an int input you can check the input using the string isdigit() method. However, there is no comparable check for float. Write a program that asks the user to enter a float and uses a try-except to check for floats by trying to convert the input string to a float and if it fails (ValueError).
Example Input
0 A 10.00
Example Output
Enter a float: \n Error int, Enter a float: \n Error not int, Enter a float: \n Error not int, Enter a float: \n Success: 10.0\n
A)
code:
f = open("numbers.txt","r")
for num in f:
if not num.isalpha():
print(num)
B)
code:
while True:
x = input("Enter a float: ")
try:
val = int(x)
print("Error int,",end=" ")
except ValueError:
try:
val = float(x)
print("Success: ",x)
break
except ValueError:
print("Error not int,",end=" ")
OUTPUT
Get Answers For Free
Most questions answered within 1 hours.