Question

In: Computer Science

Code in python Write a while loop code where it always starts form 2. Then it...

Code in python

Write a while loop code where it always starts form 2. Then it randomly chooses a number from 1-4. If the number 4 is hit then it will write “TP” if the number 1 is hit then it will write”SL”. It will rerun the program every time the numbers 1 and 5 are hit. The code should also output every single number that is randomly chosen. 2 of the same numbers can't be chosen back to back as well, so the program can't choose 2 and then 2 again.

The output should look like this:

Code Started, at number 2

Chosen number is 3

Chosen Number is 2

Chosen Number is 5

TP

Rerunning program

Code Started, at number 2

Chosen Number 1

Sl

Rerunning program

Solutions

Expert Solution

def run():
  print("code started at number 2")
  num = int(input("Choosen number is"))
  first_num = 0
  while(num <=5 and num != first_num):
    if num == 4 :
      print("TP")
      first_num = num
      num = int(input("Choosen number is"))
    elif num == 1:
      print("SL")
      first_num = num
      print("Rerunning program")
      run()
    elif num == 5:
      first_num = num
      print("Rerunning program")
      run()
    elif (num == 2 or num == 3):
      first_num = num
      num = int(input("Choosen number is"))
    else:
      break
      
    
      
run()

Output -

Given Test cases:

this is the given case tested.

In the given output when 5 is hit , it prints TP and in question you gave that TP will print when hit 4. That's why I am not printing TP when hit is 5.

If you want the case, you can add print("TP") under the condition if num == 5:

Next test cases:

if same two numbers are given and given a number other than 1 to 5:

I coded the program if these two conditions happen, program just end. but if we want to rerun the program again, simply it can run as:

def run():
  print("code started at number 2")
  num = int(input("Choosen number is"))
  while(num <=5 and ):
    if num == 4 :
      print("TP")
      num = int(input("Choosen number is"))
    elif num == 1:
      print("SL")
      print("Rerunning program")
      run()
    elif num == 5:
      print("Rerunning program")
      run()
    elif (num == 2 or num == 3):
      num = int(input("Choosen number is"))
    else:
      break
      
    
      

I think you get my points, if you need more clarification or want to satisfy more conditons please COMMENT, I will update it too.


Related Solutions

Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a sentinel value #This while loop ends when the user enters a 1, the sentinel value #Assign the number 8 to a variable which will serve as the sentinel value #Condition: while variable is not equal to 1 #Action: display the number assigned to the variable #Use an input statement (no prompt) to ask the user for a number and assign this number to the...
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
Complete the following in syntactically correct Python code. Write a program, using a for loop, that...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that calculates the amount of money a person would earn over a period of time if his or her salary is 1 penny for the first day, 2 pennies for the second day, 4 pennies for the third day, and continues to double each day. 1.      The program should ask the user for the number of days the employee worked. 2.      Display a table showing the salary...
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
Where is the infinite loop in the code? Input for the code 1 2.5 2 40200000...
Where is the infinite loop in the code? Input for the code 1 2.5 2 40200000 1 0 2 80400000 2 ffffffff 3 #include <stdio.h> #include <math.h> #include <stdlib.h> int count = 31; void toBinary(int num, int n){ for(int i = 1; i < n; i++){ if((int)(num/pow(2,(n-i))) > 0){ num = num - pow(2,(n-i)); printf("1"); count--; }else{ printf("0"); count--; } } } char checkSign (int sign) { char new_sign; if (sign == 0) { new_sign = '+'; return new_sign; }...
code in python write a code where the initial value is compounded by a multiplier, rounded...
code in python write a code where the initial value is compounded by a multiplier, rounded to the nearest tenth Output: Initial Value: 10 Multiplier: 1.4 Number of compounds: 10 Your Values are: 10 , 14, 19.6 , 27.4, 38.4, 53.8, 75.3, 105.4, 147.6, 206.6
Part A: CHALLENGE ACTIVITY 7.6.2: For loop. Printing a list. Write the Python code to accomplish...
Part A: CHALLENGE ACTIVITY 7.6.2: For loop. Printing a list. Write the Python code to accomplish the following tasks: • Input a string from the keyboard that represents three or more stock prices separated by spaces o Example: ‘34.62 76.30 85.05’ • Convert the string into a Python list of floats • Print each price in the list on a separate line with a dollar sign ($) as the first character on each line • Print the output such that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT