Asked by nick
make this simpler nd abe to run on code hs python 3 def get_float(prompt):
while True:
try:
return float(input(prompt))
except ValueError:
print("Please enter a valid number (e.g. 3.5).")
def get_int(prompt, default=10):
while True:
s = input(f"{prompt} [default {default}]: ").strip()
if not s:
return default
try:
v = int(s)
if v < 1:
print("Please enter a positive integer.")
continue
return v
except ValueError:
print("Please enter a valid integer.")
if __name__ == "__main__":
n = get_float("Enter a number: ")
max_i = get_int("How many multiples?", default=10)
width = len(str(max_i))
print(f"\nMultiplication table for {n:g} (1–{max_i}):")
for i in range(1, max_i + 1):
print(f"{n:g} x {i:>{width}} = {(n * i):g}")
All Answers 1
Answered by
GPT-5 mini
AI
Here's a simpler version you can paste into CodeHS (Python 3) and run. It keeps re-prompting for a valid number, lets you press Enter to use 10 multiples, and prints a clean multiplication table:
```
def get_float(prompt):
while True:
try:
return float(input(prompt))
except ValueError:
print("Invalid number. Try again.")
def get_int_with_default(prompt, default=10):
s = input(f"{prompt} (press Enter for {default}): ").strip()
if s == "":
return default
try:
v = int(s)
return v if v > 0 else default
except ValueError:
return default
# Main
n = get_float("Enter a number: ")
max_i = get_int_with_default("How many multiples?", 10)
print(f"\nMultiplication table for {n:g} (1–{max_i}):")
for i in range(1, max_i + 1):
print(f"{n:g} x {i} = {(n * i):g}")
```
How to use: paste into the CodeHS editor and click Run.
```
def get_float(prompt):
while True:
try:
return float(input(prompt))
except ValueError:
print("Invalid number. Try again.")
def get_int_with_default(prompt, default=10):
s = input(f"{prompt} (press Enter for {default}): ").strip()
if s == "":
return default
try:
v = int(s)
return v if v > 0 else default
except ValueError:
return default
# Main
n = get_float("Enter a number: ")
max_i = get_int_with_default("How many multiples?", 10)
print(f"\nMultiplication table for {n:g} (1–{max_i}):")
for i in range(1, max_i + 1):
print(f"{n:g} x {i} = {(n * i):g}")
```
How to use: paste into the CodeHS editor and click Run.
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.