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

Hello, I very stuck on this c++ blackjack project and was wondering if anyone can offer...
Hello, I very stuck on this c++ blackjack project and was wondering if anyone can offer help and guidance on this subject can someone help he with part 4 i have no idea how to approach the problem and how to do it I have compeleted a few parts to this project they are listed below Part 1 – Displaying Cards Write a function to display (print) a card. sample program output void displayCard(int card) Prints the name of the...
I am writing a paper on superfluidity for my physics class and was wondering if anyone...
I am writing a paper on superfluidity for my physics class and was wondering if anyone could give me some ideas of what to write on and how to write a scientific style paper. If you have any sources that could help that would be great.
(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.
I am currently working on this problem. It says it is wrong, and I was wondering...
I am currently working on this problem. It says it is wrong, and I was wondering if someone could correct my code to where its ouput is as followed. Expected Output: 6↵ 5↵ 4↵ 3↵ 2↵ 1 Actual Output: 9↵ 8↵ 7↵ 6↵ 5↵ 4↵ 3↵ 2↵ 1↵ 0↵ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values...
Hi, I have created the following code and I was wondering if it is possible to...
Hi, I have created the following code and I was wondering if it is possible to make an "if" statement in the first for loop that would output an error if the user enters a float, character or string? I was thinking of something along the lines of: if(a[i] != int) but that didn't work :( Thank you. #include <iostream> using namespace std; // Creating a constant for the number of integers in the array const int size = 10;...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT