Alice Community  

Go Back   Alice Community > Alice 3 > How Do I?

Reply
 
Thread Tools Display Modes
How to make user input calculations?
Old
plutoh
Junior Member
 
Status: Offline
Posts: 2
Join Date: Feb 2021
Default How to make user input calculations? - 02-18-2021, 11:57 AM

I want to make a calculator for Alice. So if the user puts in 4*5 it comes out with 20 etc. I also need it to pick out the largest to smallest number, for example, I put in 1,2,3,4,5 the calculator gives me 5,4,3,2,1. Then I want it to add, so 4+5+5+5=19.

I need to be able to do this with arrays I think, but I'm not sure how to do this at all and I would love some help thank you!
   
Reply With Quote
Old
yvonne910rhodes
Junior Member
 
Status: Offline
Posts: 1
Join Date: Mar 2024
Default 03-20-2024, 11:54 PM

Creating a calculator that can perform arithmetic operations and sort numbers can be achieved through programming. Here?s a simple example in Python that demonstrates how you can create such a calculator:

Code:
def calculate(expression):
    try:
        # Evaluate the arithmetic expression
        result = eval(expression)
        return result
    except Exception as e:
        return "Error: " + str(e)

def sort_numbers(numbers):
    # Convert the string of numbers into a list of integers
    number_list = [int(num) for num in numbers.split(',')]
    # Sort the numbers in descending order
    sorted_list = sorted(number_list, reverse=True)
    return ','.join(map(str, sorted_list))

# Example usage:
expression = input("Enter an arithmetic expression (e.g., 4*5): ")
print("Result:", calculate(expression))     Milestone Card

number_sequence = input("Enter a sequence of numbers separated by commas (e.g., 1,2,3,4,5): ")
print("Sorted (largest to smallest):", sort_numbers(number_sequence))
This code provides two functions: calculate which evaluates arithmetic expressions, and sort_numbers which sorts a comma-separated string of numbers in descending order. The eval function is used to evaluate the arithmetic expression, but be cautious as it can execute arbitrary code. It?s safer to use libraries like ast and operator for a production environment.

I hope the information may helps you.

Last edited by yvonne910rhodes; 03-21-2024 at 11:07 PM.
   
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2024, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.