106k views
3 votes
Which line of code will only allow a non-decimal point to be stored in a variable? candyCost = float(input("How much is the candy?")) candyCost = input("How much is the candy?") candyCost = int(input("How much is the candy?")) candyCost = str(input("How much is the candy?"))

2 Answers

1 vote

Final answer:

To store a non-decimal number, use the int() function; thus, the correct code is candyCost = int(input("How much is the candy?")).

Step-by-step explanation:

To store a non-decimal value in a variable, you should use an integer (int) data type. Hence, the correct line of code to only allow a non-decimal point number to be stored in the variable candyCost would be:

candyCost = int(input("How much is the candy?"))

This line prompts the user for input with the question 'How much is the candy?' and converts the entered value to an integer using the int() function. Note that if the user inputs a decimal value (such as 2.50), an error will be raised, as int() expects a value that can be converted into an integer without a decimal component.

User Eagspoo
by
6.6k points
3 votes

Final answer:

To store only a non-decimal number in the variable 'candyCost', use the line 'candyCost = int(input("How much is the candy?"))' which converts the input to an integer.

Step-by-step explanation:

The correct line of code that will ensure only a non-decimal number (an integer) is stored in the variable candyCost is:

candyCost = int(input("How much is the candy?"))

This line prompts the user with the question "How much is the candy?" and then converts the input from the user into an integer. If the input cannot be converted to an integer (for example, if the user enters a decimal number), Python will raise a ValueError. Using int() is key to ensuring that only whole numbers without decimals are accepted.

User Kliment Ru
by
5.9k points