简体   繁体   中英

While statement and def function

I'm learning the def function and wrote this code:

def thirdpower(number):
    n=number**3
    print "%d to the 3rd power is %d" % (number,n)
thirdpower(input("Enter a number"))

But now, I want to add in a conditional statement (perhaps using the while statement) so that if a user leaves the input blank, it will print out an error, and will re-ask the user.

So far, I can't figure it out:

def thirdpower(number):
        n=number**3
while True:
    if len(thirdpower())==0:
        print "%d to the 3rd power is %d" % (number,n)
        thirdpower(input("Enter a number"))
    break
else:
    print "you entered a blank, please try again."

Any input is apreciated!! :)

Not sure I know exactly what you are up to, but this might help:

def thirdpower(n):
    return n**3
number = None
while not number:
    try:
        number = float(raw_input("Enter a number "))
    except:
        number = None
print "%d to the 3rd power is %d" % (number, thirdpower(number))

first of all, having a break statement in your while loop is a bad practice(not to mention while True ), you should always focus on having a condition that will quit itself

you can do something like this

user_input = input("enter a number")
while user_input == "" : #or other conditions you want to put
    print("error message")
    user_input = input("enter a number")

如果您的数字将是来自用户的raw_input,则可以在进入while循环之前分配数字,或者将参数传递给函数来处理该数字。

What you want is this:

while True:
    try:
        number = int(input('Enter a number: '))
        break
    except ValueError:
        print('Please enter an integer.')

print('%d to the 3rd power is %d' % (number, thirdpower(number)))

In a try statement, if something goes wrong it looks for an except block. So in your case, when a user enters something that cannot be converted to an integer such as a non-number string or whitespace, it will fail and produce a ValueError . It will then look for the corresponding except block, and execute the statements nested within.

As it is in a while loop, this will keep happening until the input is found to be valid (by no exceptions occurring) and the while loop is broken, with number being the last entered value.

Python functions are recursive, use this functionnality :

def thirdpower():
    try:
        number = int(raw_input("Enter a number : "))
        n=number**3
        print "%d to the 3rd power is %d" % (number,n)
    except ValueError:
        print "You must enter an integer, please try again."
        thirdpower()

thirdpower()

The try: ... except:... blocks is used to determine if the value is an integer or not.

Try breaking up the problem in to multiple parts, getting the value and calculating the "thirdpower".

You will also want to make sure to convert your user input into an integer.

while True:
    try:
        user_val = int(raw_input("Enter a number: "))
        break
    except ValueError:
        print "You didn't enter a valid integer"
thirdpower(user_val)

If you want float values just change "int" to "float". You may also want to return the value from thirdpower :

def thirdpower(number):
    n = number**3
    print "%d to the 3rd power is %d" % (number, n)
    return n

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM