Question

In: Computer Science

This is a Python project and I´m wondering if anyone has a simple solution for this?...

This is a Python project and I´m wondering if anyone has a simple solution for this?

Write a program, move.py, which asks the user for the position of a virtual character on the x-axis in a coordinate system and then allows the user to move the virtual character to the right or left, indicated by the characters 'r' or 'l'. The user should be able to move the virtual character as often as he/she wishes, but if the input is neither 'r' nor 'l' the program quits. Instructions for the user are written out at the beginning, but the position of the virtual character (denoted by 'o') is shown in each iteration.

The valid range on the x-axis, which the virtual character can traverse, is from 1 to 10 and you can assume that at the beginning the user inputs a number in this range. If the virtual character is positioned at either end of the range it will not move when the user tries to move it out of the range.

In your solution, you should only use methods/material discussed in the first five chapters in the textbook. This means, for example, that you are not allowed to use lists.

Make sure that your code is very readable (see, for example, this thread).

Note the following:

  • The numbers 1 and 10, which signify the range, should be implemented as constants. In Python, a constant is a variable whose value will not change in the program. It is a good rule to use capital letters for naming constants.
  • You must use functions in your implementation. It is fine that your main program contains the loop, but the body of the loop should, in most part, be a series of function calls.
  • Example input/output is below. Your output should be exactly the same.

Example input/output:

Input a position between 1 and 10: 7
xxxxxxoxxx
l - for moving left
r - for moving right
Any other letter for quitting
Input your choice: r
xxxxxxxoxx
Input your choice: r
xxxxxxxxox
Input your choice: r
xxxxxxxxxo
Input your choice: r
xxxxxxxxxo
Input your choice: l
xxxxxxxxox
Input your choice: l
xxxxxxxoxx
Input your choice: l
xxxxxxoxxx
Input your choice: l
xxxxxoxxxx
Input your choice: q
xxxxxoxxxx

Solutions

Expert Solution

The program is as below

# define constant for the start and end position range
START=1
END=10
virtual_chr = "o"
position = int(input("Input a position between %s and %s: "%(START,END)))
print_str = 'x'*(position-START) + virtual_chr + 'x'*(END-position)
# print the string
print(print_str)
print("l - for moving left")
print("r - for moving right")
print("Any other letter for quitting")

# loop of the game
while True:
move = input("Input your choice: ")
# If 'l' is entered
if move == 'l' :
if position != START:
position = position - 1
# If 'r' is entered
elif move == 'r':
if position != END:
position = position + 1
# need to quit game
else:
# print the string before quit
print(print_str)
break
# set new print_str
print_str = 'x'*(position-START) + virtual_chr + 'x'*(END-position)
# print the string
print(print_str)

  
Here is the program output:

Input a position between 1 and 10: 7
xxxxxxoxxx
l - for moving left
r - for moving right
Any other letter for quitting
Input your choice: r
xxxxxxxoxx
Input your choice: r
xxxxxxxxox
Input your choice: r
xxxxxxxxxo
Input your choice: r
xxxxxxxxxo
Input your choice: l
xxxxxxxxox
Input your choice: l
xxxxxxxoxx
Input your choice: l
xxxxxxoxxx
Input your choice: l
xxxxxoxxxx
Input your choice: q
xxxxxoxxxx



Related Solutions

(JAVA) I was wondering if anyone can check if I am implementing this method correctly based...
(JAVA) I was wondering if anyone can check if I am implementing this method correctly based on the instructions? Here are the instructions: buildShape(String input) In the buildShape() method, you will parse the provided input to determine whether to create a CircleShape or SquareShape. Before you create the shape, you need to determine it's size, location and color. Use TestableRandom to randomly generate an int size ranging from 100-200. Randomly generate an x and y index for its location. X...
Hi, I was wondering if anyone could explain what capital asset pricing is? My homework suggests...
Hi, I was wondering if anyone could explain what capital asset pricing is? My homework suggests that the capital asset pricing model considers the relationship between the fluctuations in a security’s returns versus the markets. When explaining, please provide an example as that would help me understand better! Thank you!
I have a question pertaining to ruby on rails. I was wondering if there is a...
I have a question pertaining to ruby on rails. I was wondering if there is a way to set aspect ratios for different monitor sizes for images used on a page. Thanks for any help.
what is the solution of this code? Also how would I run it in python to...
what is the solution of this code? Also how would I run it in python to check? q = Queue() q.enqueue(10) q.enqueue(12) q.enqueue(15) for i in range(len(q)): → if len(q) % 2 == 0: → → q.enqueue(q.dequeue()) → else: → → q.dequeue() print(q)
hello, I was wondering if I can see an example of a statistics prop1ztest. and a...
hello, I was wondering if I can see an example of a statistics prop1ztest. and a bit more information about when to use it.
Can anyone assist me with the following problem posted below? I just need some simple use...
Can anyone assist me with the following problem posted below? I just need some simple use case diagrams made for all the systems at play as described, ill do my best to answer any questions you may have, thank you! SYSTEM DETAILS: The warehouse stores items for purchase by customers. To expedite order fulfillment, stock of most items is kept and a minimum reorder is made when stocks are below a set trigger quantity. Items ordered that are not in...
I was wondering if I was doing a conversion right. I am supposed to take aqueous...
I was wondering if I was doing a conversion right. I am supposed to take aqueous HNO3 with a density of 1.42 g/mL and a mass percent of solution of 70% and convert that into molarity. Would the correct conversion be to take 70 g of HNO3 over 100 g of H2O multiplied by 1 mol HNO3 over 63.01 g HNO3 (the molar mass) and then multiply that by 1 g H2O over 0.001 L of H2O to get the...
I have done a fcff in excel and i was wondering how do i preform a...
I have done a fcff in excel and i was wondering how do i preform a sensitivity analysis with it. I did the FCFF but need help with the sensitivity analysis. Here is the question: You are thinking of investing $1000 in a machine today. It would be used to produce sales for 10 years. The projected sales number in year 1 is 1200, after which sales will grow at 2% annually. Cost of manufacturing for this project is 50%...
Exam prep question I am struggling with. Could anyone give me a solution with steps? Thank...
Exam prep question I am struggling with. Could anyone give me a solution with steps? Thank you! You are an executive at WX Resorts and you have executive stock options as part of your compensation. You hold 1,000 options that expire six months from today at a strike price of 50 but that can be exercised early. WX is trading at $55 and just announced a $1 dividend per share that will be paid four months from today to stockholders...
I have to do a simple linear regression project. The dependent variable that I chose is...
I have to do a simple linear regression project. The dependent variable that I chose is international tourist receipts (US$ billions) and the independent variable I chose is International tourist arrivals in (billions). Did I set up my variables correctly? I am using data from the World Bank. The main trouble I am having is why testing the correlation between these two variables is important and need some ideas on answering the few questions in the introduction of the essay,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT