Python: If...and... not working as expected

MK2009

I'm trying to set up the below code to ask the user to enter two integers (num_sides & num_rolls). If the user doesn't enter an integer for either input, then the code should print the statement "Enter an integer".

I find this code only tests the num_sides but doesn't test if num_rolls is an integer. What's going on here?

Thanks in advance for your help!

def rolldice():
    while True:
        while True:
            num_sides = input("Enter number of sides for die: ")
            num_rolls = input("Enter number of rolls for die: ")
            try:
                if int(num_sides) != num_sides or int(num_rolls) != num_rolls:
                    break
                break
            except(ValueError):
                print("Enter an integer")
                True
jeromej

Why do you have a nested loop? (Two while True one inside the other one)

The following code is simpler and should work:

def rolldice():
    while True:
        num_sides = input("Enter number of sides for die: ")
        num_rolls = input("Enter number of rolls for die: ")
        try:
            int(num_sides)
            int(num_rolls)
            break
        except ValueError:
            print("Enter an integer")

If both int evaluate and do not crash then break is executed and you can leave the loop.

As it is a function, you might want to return (num_sides, num_rolls) at the end of the function so they aren't lost. (You could also replace the break with the return, which will effectively stop the function too at the same time)

Unless this is only the beginning of your code, then never mind what I just said. :)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Python dice game not working as expected

分類Dev

Python counter values and boolean all() not working as expected

分類Dev

Python - if, elif, elif, else Statement Not Working as Expected

分類Dev

Python - 'in' keyword and if-statements not working as expected

分類Dev

AJAX call is not working as expected

分類Dev

jQuery .when().then() not working as expected

分類Dev

.vimrc file not working as expected

分類Dev

*ngIf not working as expected with observable

分類Dev

ItemIsAutoTristate flag not working as expected

分類Dev

KeyboardAvoidingView not working as expected on IOS

分類Dev

PowerShell variables not working as expected

分類Dev

XPath logical 'and' not working as expected?

分類Dev

CancellationTokenSource not working as expected on a TaskCompletionSource

分類Dev

string formatting not working as expected

分類Dev

Why is .on() Not working as expected in jquery?

分類Dev

Tuples in Scala not working as expected

分類Dev

strpos() not working as expected

分類Dev

PHP Dateformat not working as expected

分類Dev

Update not working as expected

分類Dev

ifelse not working as expected in R

分類Dev

$("#form").submit(); is not working as expected

分類Dev

Prototype is not working as expected

分類Dev

LIKE and Equals not working as expected

分類Dev

Regex is not working as expected in javascript

分類Dev

angularjs service not working as expected

分類Dev

NSTask not working as expected / hoped for

分類Dev

Routing not working as expected in CodeIgniter

分類Dev

mhddfs not working as expected

分類Dev

timeout for ngShow not working as expected