Learn the basics of Python 3-Code Challenges:Control Flow

1.Coding question 1

You are given two numbers stored in num1 and num2. If the sum of num1 and num2 is NOT equal to 10, then store True into a variable called not_ten, otherwise store False in not_ten.

num1 = 6
num2 = 3

# Write your if statement here
if num1 + num2 != 10:
    not_ten = True
else:
    not_ten = False

# show the result
print("Does the sum of the numbers equal 10? " + str(not_ten))

 2.Coding question 2

You are given a monthly budget and some expenses and need to check if the sum of the expenses goes over budget!
First, store the total of all expenses into a variable called total.
Next, check if the total is greater than the budget. If it is, store True into a variable called over_budget, otherwise store False in over_budget.

# Monthly budget
budget = 2000

# Monthly expenses
food_bill = 200
electricity_bill = 100
internet_bill = 60
rent = 1500

# Calculate the total amount of expenses
total = food_bill + electricity_bill + internet_bill + rent

# Check if the total is greater than the budget and store the result in over_budget
if total > budget:
    over_budget = True
else:
    over_budget = False

# see the results

print("Total: " + str(total))
print("Is it over budget? " + str(over_budget))

 3.Coding question 3

Create a function named large_power() that takes two parameters named base and exponent.

If base raised to the exponent is greater than 5000, return True, otherwise return False.

def large_power(base, exponent):
    if base ** exponent > 5000:
        return True
    else:
        return False


print(large_power(2, 13))
# should print True

print(large_power(2, 12))
# should print False

4.Coding question 4

Create a function named twice_as_large() that has two parameters named num1 and num2.
Return True if num1 is more than double num2. Return False otherwise.

# Write your twice_as_large function here:
def twice_as_large(num1, num2):
    if num1 > num2 * 2:
        return True
    else:
        return False


print(twice_as_large(10, 5))
# should print False

print(twice_as_large(11, 5))
# should print True

 5.Coding question 5

Create a function called divisible_by_ten() that has one parameter named num.

The function should return True if num is divisible by 10, and False otherwise. Consider using modulo operator % to check for divisibility.

# Write your divisible_by_ten() function here:
def  divisible_by_ten(num):
  if num % 10 == 0:
    return True
  else:
    return False


print(divisible_by_ten(20))
# should print True

print(divisible_by_ten(25))
# should print False

Learn the basics of Python 3-Chapter 2:Control Flow
Learn the basics of Python 3-Code Challenges:Control Flow (Advanced)

  • 13
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮猴的路数

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值