0% found this document useful (0 votes)
16 views2 pages

BANA200Week2.ipynb - Colaboratory

The document contains code snippets and exercises related to Python programming concepts like functions, loops, conditionals, and debugging code. The first exercise asks the user to create a lottery number checking program. The second asks to write code to convert a test score to a letter grade. Other snippets demonstrate using for loops to count items in a list, compute a sum, and find the largest/smallest value. The last part defines a function to check if a number is even.

Uploaded by

Aarti Barve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

BANA200Week2.ipynb - Colaboratory

The document contains code snippets and exercises related to Python programming concepts like functions, loops, conditionals, and debugging code. The first exercise asks the user to create a lottery number checking program. The second asks to write code to convert a test score to a letter grade. Other snippets demonstrate using for loops to count items in a list, compute a sum, and find the largest/smallest value. The last part defines a function to check if a number is even.

Uploaded by

Aarti Barve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

12/09/2023, 20:16 BANA200Week2.

ipynb - Colaboratory

Exercise 1: Lottery Winning Number Checking

You have been asked to create a program with the following functions:

Ask user for their lottery ticket number. If the input matches the winning number, 8918354118, then print a message, “Congratulations,
you are the winner!”
If the input does not match the winning number, then print, “Sorry, you are not the winner.”
Print “Thank you for using my system!” to all users.

1 # Please enter your code below

Exercise 2: Letter Grade Conversion

Ask Professor to enter a score between 0 and 100. If the score is out of range, print an error message. If the score is between 0 and 100, print a
grade using the following table:

if >=90: A
if >=80: B
if >=70: C
if <70: F

1 # How would you debug the following code?


2
3 score = input("Please enter a score: ")
4 if score >= 0 or score <= 100
5 if score >= 90:
6 lettergrade = 'A'
7 elif score >= 80:
8 lettergrade = 'B'
9 elif score >= 70:
10 lettergrade = 'C'
11 elif score >= 60:
12 lettergrade = 'D'
13 else
14 lettergrade = 'F'
15 print(f"The letter grade is: {lettergrade}.")
16 else
17 print('Bad Score!')
18

1 # Using for loop to count the number of items in a list: [3, 41, 12, 9, 74, 15]
2 count = 0
3 for itervar in [3, 41, 12, 9, 74, 15]:
4 count = count + 1
5 print('Count: ', count)

Count: 6

1 # Using for loop to compute the sum of all numbers in the list
2 mysum = 0
3 for itervar in [3, 41, 12, 9, 74, 15]:
4 mysum = mysum + itervar
5 print("Sum is : ", mysum)

Sum is : 154

1 # Using for loop to find the largest/smallest value in the list


2 def is_largest(i):
3 input: i is largest int
4 print("leagest number: ")
5 return i

File "<ipython-input-15-00cfe3b8e629>", line 3


input: i is largest int
^
SyntaxError: invalid syntax

SEARCH STACK OVERFLOW

1 # Functions
2 def is_even( i):
3 """
4 Input: i, a positive int
5 Returns True if i is even, otherwise False

https://colab.research.google.com/drive/1VluN0xmP5qa2hKpsn885ieMBJNN665qI#scrollTo=tUKq3zz3LeDW&printMode=true 1/2
12/09/2023, 20:16 BANA200Week2.ipynb - Colaboratory
6 """
7 print("This is an even number?")
8 return i%2 == 0
9
10
11 x=3
12 is_even(x)
13

Colab paid products - Cancel contracts here

https://colab.research.google.com/drive/1VluN0xmP5qa2hKpsn885ieMBJNN665qI#scrollTo=tUKq3zz3LeDW&printMode=true 2/2

You might also like