2

Ask the user to keep entering one number after another until the user inputs 0.

For each number the user gives, print its cube, square and, print whether it's odd or even.

The program I made is:

num = int(input("Enter number: "))
correct_num = 0
square = num ** 2
print("The square is:", square)
cube = num ** 3
print("The cube is: ", cube)
if (num % 2) == 0:
    print(num, "is an odd number")
else:
    print(num, "is an odd number")
while num == correct_num:
    break

This code will only ask the user to input a number once, while I need the number to be asked until the number given is 0. I can't understand how to make a loop to continuously ask the user to input number until it's 0.

Red
  • 26,798
  • 7
  • 36
  • 58
Sara
  • 43
  • 5
  • 1
    Please show what you’ve researched/tried so far, regarding “I need the number to be asked until the number given is 0”. Where are you stuck? – S3DEV Sep 05 '20 at 15:56
  • this will help https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – deadshot Sep 05 '20 at 15:56
  • I can't understand how to make a loop to continuously ask the user to input number until it's 0. – Sara Sep 05 '20 at 15:59
  • There's also a logical error in your code if num%2 equals 0, then the number is a even number, else its odd. But in your code, the output is "is an odd number", in both cases – John Anchery Sep 05 '20 at 16:24
  • @Sara Note that the answer you've used will make the program print the square and cube of the input number when the number input is `0`. – Red Sep 05 '20 at 21:49

2 Answers2

1

Instead of doing what you did, you need to wrap the whole thing in a while loop, it would look something like the following depending on what you are doing:

num = 2
while num != 0:
    num=int(input("Enter number: "))
    square=num**2
    print("The square is: ",square)
    cube=num**3
    print("The cube is: ",cube)
    if (num%2)==0:
        print(num,"is an odd number")
    else:
        print(num,"is an odd number")

print("0 has been typed")
AyanSh
  • 324
  • 1
  • 10
0

Use break when the condition is met:

correct_num = 0

while True:
    num = int(input("Enter number: "))
    if num == correct_num:
        break
    square = num ** 2
    print("The square is: ", square)
    cube = square * num
    print("The cube is: ", cube)
    if num % 2:
        print(num, "is an odd number")
    else:
        print(num, "is an odd number")
Red
  • 26,798
  • 7
  • 36
  • 58